Dado un número entero N y una array N x N , la tarea es convertir la array dada en una array simétrica reemplazando (i, j) th y (j, i) th elemento con su media aritmética .
Ejemplos:
Entrada: arr[] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Salida:
1 3 5
3 5 7
5 7 9
Explicación: Los elementos de la diagonal son iguales. El elemento en el índice (0, 1) = 2 y (1, 0) = 4 se reemplaza por su media aritmética, es decir, (2 + 4) / 2 = 3. De manera similar, los elementos en el índice (2, 0) y ( 0, 2), (2, 1) y (1, 2) también se reemplazan por su media aritmética y la array de salida resultante es una array simétrica.Entrada: arr[] = {{12, 43, 65},
{23, 75, 13},
{51, 37, 81}}
Salida:
12 33 58
33 75 25
58 25 81
Enfoque: El problema dado es un problema basado en la implementación. La idea es recorrer la array triangular inferior y reemplazar los elementos y sus respectivos índices de transposición con su media aritmética .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program of the above approach #include <iostream> using namespace std; const int N = 3; // Function to convert the given matrix // into a symmetric matrix by replacing // transpose elements with their mean void makeSymmetric(int mat[][N]) { // Loop to traverse lower triangular // elements of the given matrix for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (j < i) { mat[i][j] = mat[j][i] = (mat[i][j] + mat[j][i]) / 2; } } } } // Function to print the given matrix void showMatrix(int mat[][N]) { // Loop to traverse the // given matrix for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { // Print current index cout << mat[i][j] << " "; } cout << "\n"; } } // Driver Code int main() { int arr[][N] = { { 12, 43, 65 }, { 23, 75, 13 }, { 51, 37, 81 } }; makeSymmetric(arr); showMatrix(arr); return 0; }
Java
// Java program of the above approach import java.util.*; class GFG{ static int N = 3; // Function to convert the given matrix // into a symmetric matrix by replacing // transpose elements with their mean static void makeSymmetric(int mat[][]) { // Loop to traverse lower triangular // elements of the given matrix for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { if (j < i) { mat[i][j] = mat[j][i] = (mat[i][j] + mat[j][i]) / 2; } } } } // Function to print the given matrix static void showMatrix(int mat[][]) { // Loop to traverse the // given matrix for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { // Print current index System.out.print(mat[i][j] + " "); } System.out.println(); } } // Driver Code public static void main(String args[]) { int arr[][] = { { 12, 43, 65 }, { 23, 75, 13 }, { 51, 37, 81 } }; makeSymmetric(arr); showMatrix(arr); } } // This code is contributed by sanjoy_62
Python3
# python3 program of the above approach N = 3 # Function to convert the given matrix # into a symmetric matrix by replacing # transpose elements with their mean def makeSymmetric(mat): # Loop to traverse lower triangular # elements of the given matrix for i in range(0, N): for j in range(0, N): if (j < i): mat[i][j] = mat[j][i] = (mat[i][j] + mat[j][i]) // 2 # Function to print the given matrix def showMatrix(mat): # Loop to traverse the # given matrix for i in range(0, N): for j in range(0, N): # Print current index print(mat[i][j], end=" ") print() # Driver Code if __name__ == "__main__": arr = [[12, 43, 65], [23, 75, 13], [51, 37, 81]] makeSymmetric(arr) showMatrix(arr) # This code is contributed by rakeshsahni
C#
// C# program of the above approach using System; public class GFG { static int N = 3; // Function to convert the given matrix // into a symmetric matrix by replacing // transpose elements with their mean static void makeSymmetric(int [,]mat) { // Loop to traverse lower triangular // elements of the given matrix for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { if (j < i) { mat[i,j] = mat[j,i] = (mat[i,j] + mat[j,i]) / 2; } } } } // Function to print the given matrix static void showMatrix(int [,]mat) { // Loop to traverse the // given matrix for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { // Print current index Console.Write(mat[i, j] + " "); } Console.WriteLine(); } } // Driver Code public static void Main(String []args) { int [,]arr = { { 12, 43, 65 }, { 23, 75, 13 }, { 51, 37, 81 } }; makeSymmetric(arr); showMatrix(arr); } } // This code is contributed by 29AjayKumar
Javascript
<script> // JavaScript code for the above approach let N = 3; // Function to convert the given matrix // into a symmetric matrix by replacing // transpose elements with their mean function makeSymmetric(mat) { // Loop to traverse lower triangular // elements of the given matrix for (let i = 0; i < N; i++) { for (let j = 0; j < N; j++) { if (j < i) { mat[i][j] = mat[j][i] = Math.floor((mat[i][j] + mat[j][i]) / 2); } } } } // Function to print the given matrix function showMatrix(mat) { // Loop to traverse the // given matrix for (let i = 0; i < N; i++) { for (let j = 0; j < N; j++) { // Print current index document.write(mat[i][j] + " "); } document.write('<br>') } } // Driver Code let arr = [[12, 43, 65], [23, 75, 13], [51, 37, 81]]; makeSymmetric(arr); showMatrix(arr); // This code is contributed by Potta Lokesh </script>
12 33 58 33 75 25 58 25 81
Complejidad temporal: O(N 2 )
Complejidad espacial: O(1)
Publicación traducida automáticamente
Artículo escrito por pintusaini y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA