Se dice que una array cuadrada es una array simétrica si la transpuesta de la array es la misma que la array dada. La array simétrica se puede obtener cambiando fila a columna y columna a fila.
Ejemplos:
Input : 1 2 3 2 1 4 3 4 3 Output : Yes Input : 3 5 8 3 4 7 8 5 3 Output : No
Una solución simple es hacer lo siguiente.
- Crear transposición de array dada.
- Compruebe si las arrays transpuestas y dadas son iguales o no,
Implementación:
C++
// Simple c++ code for check a matrix is // symmetric or not. #include <iostream> using namespace std; const int MAX = 100; // Fills transpose of mat[N][N] in tr[N][N] void transpose(int mat[][MAX], int tr[][MAX], int N) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) tr[i][j] = mat[j][i]; } // Returns true if mat[N][N] is symmetric, else false bool isSymmetric(int mat[][MAX], int N) { int tr[N][MAX]; transpose(mat, tr, N); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (mat[i][j] != tr[i][j]) return false; return true; } // Driver code int main() { int mat[][MAX] = { { 1, 3, 5 }, { 3, 2, 4 }, { 5, 4, 1 } }; if (isSymmetric(mat, 3)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Simple java code for check a matrix is // symmetric or not. import java.io.*; class GFG { static int MAX = 100; // Fills transpose of mat[N][N] in tr[N][N] static void transpose(int mat[][], int tr[][], int N) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) tr[i][j] = mat[j][i]; } // Returns true if mat[N][N] is symmetric, else false static boolean isSymmetric(int mat[][], int N) { int tr[][] = new int[N][MAX]; transpose(mat, tr, N); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (mat[i][j] != tr[i][j]) return false; return true; } // Driver code public static void main (String[] args) { int mat[][] = { { 1, 3, 5 }, { 3, 2, 4 }, { 5, 4, 1 } }; if (isSymmetric(mat, 3)) System.out.println( "Yes"); else System.out.println ( "No"); } }
Python
# Simple Python code for check a matrix is # symmetric or not. # Fills transpose of mat[N][N] in tr[N][N] def transpose(mat, tr, N): for i in range(N): for j in range(N): tr[i][j] = mat[j][i] # Returns true if mat[N][N] is symmetric, else false def isSymmetric(mat, N): tr = [ [0 for j in range(len(mat[0])) ] for i in range(len(mat)) ] transpose(mat, tr, N) for i in range(N): for j in range(N): if (mat[i][j] != tr[i][j]): return False return True # Driver code mat = [ [ 1, 3, 5 ], [ 3, 2, 4 ], [ 5, 4, 1 ] ] if (isSymmetric(mat, 3)): print "Yes" else: print "No" # This code is contributed by Sachin Bisht
C#
// Simple C# code for check a matrix is // symmetric or not. using System; class GFG { static int MAX = 100; // Fills transpose of mat[N][N] in tr[N][N] static void transpose(int [,]mat, int [,]tr, int N) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) tr[i,j] = mat[j,i]; } // Returns true if mat[N][N] is symmetric, else false static bool isSymmetric(int [,]mat, int N) { int [,]tr = new int[N,MAX]; transpose(mat, tr, N); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (mat[i,j] != tr[i,j]) return false; return true; } // Driver code public static void Main () { int [,]mat = { { 1, 3, 5 }, { 3, 2, 4 }, { 5, 4, 1 } }; if (isSymmetric(mat, 3)) Console.WriteLine( "Yes"); else Console.WriteLine( "No"); } } // This code is contributed by vt_m.
PHP
<?php // Simple PHP code for check a matrix is // symmetric or not. // Returns true if mat[N][N] is // symmetric, else false function isSymmetric($mat, $N) { $tr = array(array()); for ($i = 0; $i < $N; $i++) for ($j = 0; $j < $N; $j++) $tr[$i][$j] = $mat[$j][$i]; // Fills transpose of // mat[N][N] in tr[N][N] for ($i = 0; $i < $N; $i++) for ($j = 0; $j < $N; $j++) if ($mat[$i][$j] != $tr[$i][$j]) return false; return true; } // Driver code $mat= array(array(1, 3, 5), array(3, 2, 4), array(5, 4, 1)); if (isSymmetric($mat, 3)) echo "Yes"; else echo "No"; // This code is contributed by Sam007 ?>
Javascript
<script> // Simple javascript code for check // a matrix is symmetric or not. let MAX = 100; // Fills transpose of mat[N][N] in tr[N][N] function transpose(mat, tr, N) { for(let i = 0; i < N; i++) for(let j = 0; j < N; j++) tr[i][j] = mat[j][i]; } // Returns true if mat[N][N] is // symmetric, else false function isSymmetric(mat, N) { let tr = new Array(N); for(let i = 0; i < N; i++) { tr[i] = new Array(MAX); } transpose(mat, tr, N); for(let i = 0; i < N; i++) for(let j = 0; j < N; j++) if (mat[i][j] != tr[i][j]) return false; return true; } // Driver code let mat = [ [ 1, 3, 5 ], [ 3, 2, 4 ], [ 5, 4, 1 ] ]; if (isSymmetric(mat, 3)) document.write("Yes"); else document.write("No"); // This code is contributed by decode2207 </script>
Yes
Complejidad temporal: O(N x N)
Espacio auxiliar: O(N x N)
Una solución eficiente para verificar si una array es simétrica o no es comparar los elementos de la array sin crear una transposición. Básicamente necesitamos comparar mat[i][j] con mat[j][i].
Implementación:
C++
// Efficient c++ code for check a matrix is // symmetric or not. #include <iostream> using namespace std; const int MAX = 100; // Returns true if mat[N][N] is symmetric, else false bool isSymmetric(int mat[][MAX], int N) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (mat[i][j] != mat[j][i]) return false; return true; } // Driver code int main() { int mat[][MAX] = { { 1, 3, 5 }, { 3, 2, 4 }, { 5, 4, 1 } }; if (isSymmetric(mat, 3)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Efficient Java code for check a matrix is // symmetric or no import java.io.*; class GFG { static int MAX = 100; // Returns true if mat[N][N] // is symmetric, else false static boolean isSymmetric(int mat[][], int N) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (mat[i][j] != mat[j][i]) return false; return true; } // Driver code public static void main (String[] args) { int mat[][] = { { 1, 3, 5 }, { 3, 2, 4 }, { 5, 4, 1 } }; if (isSymmetric(mat, 3)) System.out.println( "Yes"); else System.out.println("NO"); } } // This article is contributed by vt_m.
Python
# Efficient Python code for check a matrix is # symmetric or not. # Returns true if mat[N][N] is symmetric, else false def isSymmetric(mat, N): for i in range(N): for j in range(N): if (mat[i][j] != mat[j][i]): return False return True # Driver code mat = [ [ 1, 3, 5 ], [ 3, 2, 4 ], [ 5, 4, 1 ] ] if (isSymmetric(mat, 3)): print "Yes" else: print "No" # This code is contributed by Sachin Bisht
C#
// Efficient C# code for check a matrix is // symmetric or no using System; class GFG { //static int MAX = 100; // Returns true if mat[N][N] // is symmetric, else false static bool isSymmetric(int [,]mat, int N) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (mat[i, j] != mat[j, i]) return false; return true; } // Driver code public static void Main () { int [,]mat = { { 1, 3, 5 }, { 3, 2, 4 }, { 5, 4, 1 } }; if (isSymmetric(mat, 3)) Console.WriteLine( "Yes"); else Console.WriteLine("NO"); } } // This code is contributed by vt_m.
PHP
<?php // Efficient PHP code for // check a matrix is // symmetric or not. $MAX = 100; // Returns true if mat[N][N] // is symmetric, else false function isSymmetric($mat, $N) { for ($i = 0; $i < $N; $i++) for ($j = 0; $j < $N; $j++) if ($mat[$i][$j] != $mat[$j][$i]) return false; return true; } // Driver code $mat = array(array(1, 3, 5), array(3, 2, 4), array(5, 4, 1)); if (isSymmetric($mat, 3)) echo("Yes"); else echo("No"); // This code is contributed by Ajit. ?>
Javascript
<script> // Efficient Javascript code for check a matrix is symmetric or no let MAX = 100; // Returns true if mat[N][N] // is symmetric, else false function isSymmetric(mat, N) { for (let i = 0; i < N; i++) for (let j = 0; j < N; j++) if (mat[i][j] != mat[j][i]) return false; return true; } let mat = [ [ 1, 3, 5 ], [ 3, 2, 4 ], [ 5, 4, 1 ] ]; if (isSymmetric(mat, 3)) document.write( "Yes"); else document.write("NO"); </script>
Yes
Complejidad temporal: O(N x N)
Espacio auxiliar: O(1)
Este artículo es una contribución de Dharmendra kumar . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA