Dada una array mat[][] de dimensiones N*M , la tarea es comprobar si todas las diagonales de la array (de arriba a la derecha a abajo a la izquierda) son palindrómicas o no. Si se encuentra que es cierto , escriba Sí . De lo contrario , imprima No.
Ejemplos:
Entrada: mat[][] = [[1, 0, 0, 0], [0, 1, 1, 1], [0, 1, 0, 1], [0, 1, 1, 0]]
Salida : Sí
Explicación:
Todas las diagonales de la array mat[][] están dadas por:
- {1}
- {0, 0}
- {0, 1, 0}
- {0, 1, 1, 0}
- {1, 0, 1}
- {1, 1}
- {0}
Como todas las diagonales anteriores son palindrómicas. Por lo tanto, imprima Sí.
Entrada: mat[][] = [[1, 0, 0, 0], [1, 1, 0, 1], [1, 0, 1, 1], [0, 1, 0, 1]]
Salida : No
Enfoque: El problema dado se puede resolver realizando el recorrido diagonal de la array y para cada recorrido diagonal verificar si los elementos son palindrómicos o no. Si existe tal diagonal que no sea palindrómica, imprima Sí . De lo contrario , imprima No.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; #define N 5 // Function to check if the matrix is // palindrome or not string isbinaryMatrixPalindrome( int mat[N][N]) { // Traverse the matrix and check if // top right and bottom left elements // have same value for (int i = 0; i < N - 1; i++) { for (int j = N - 1; j > i; j--) { // If top right element is not // equal to the bottom left // element return false if (mat[i][j] != mat[j][i]) { return "Np"; } } } return "Yes"; } // Driver Code int main() { int mat[N][N] = { { 1, 0, 0, 1, 1 }, { 0, 1, 0, 1, 0 }, { 0, 0, 1, 1, 1 }, { 1, 1, 1, 0, 1 }, { 1, 0, 1, 1, 0 } }; cout << isbinaryMatrixPalindrome(mat); return 0; }
Java
// Java program for the above approach public class GFG { final static int N = 5; // Function to check if the matrix is // palindrome or not static String isbinaryMatrixPalindrome(int mat[][]) { // Traverse the matrix and check if // top right and bottom left elements // have same value for (int i = 0; i < N - 1; i++) { for (int j = N - 1; j > i; j--) { // If top right element is not // equal to the bottom left // element return false if (mat[i][j] != mat[j][i]) { return "Np"; } } } return "Yes"; } // Driver Code public static void main (String[] args) { int mat[][] = { { 1, 0, 0, 1, 1 }, { 0, 1, 0, 1, 0 }, { 0, 0, 1, 1, 1 }, { 1, 1, 1, 0, 1 }, { 1, 0, 1, 1, 0 } }; System.out.println(isbinaryMatrixPalindrome(mat)); } } // This code is contributed by AnkThon
Python3
# python program for the above approach N = 5 # Function to check if the matrix is # palindrome or not def isbinaryMatrixPalindrome(mat): # Traverse the matrix and check if # top right and bottom left elements # have same value for i in range(0, N - 1): for j in range(N - 1, i, -1): # If top right element is not # equal to the bottom left # element return false if (mat[i][j] != mat[j][i]): return "No" return "Yes" # Driver Code if __name__ == "__main__": mat = [[1, 0, 0, 1, 1], [0, 1, 0, 1, 0], [0, 0, 1, 1, 1], [1, 1, 1, 0, 1], [1, 0, 1, 1, 0]] print(isbinaryMatrixPalindrome(mat)) # This code is contributed by rakeshsahni
C#
// C# program for the above approach using System; public class GFG { static int N = 5; // Function to check if the matrix is // palindrome or not static string isbinaryMatrixPalindrome(int [,]mat) { // Traverse the matrix and check if // top right and bottom left elements // have same value for (int i = 0; i < N - 1; i++) { for (int j = N - 1; j > i; j--) { // If top right element is not // equal to the bottom left // element return false if (mat[i, j] != mat[j, i]) { return "Np"; } } } return "Yes"; } // Driver Code public static void Main (string[] args) { int [,]mat = { { 1, 0, 0, 1, 1 }, { 0, 1, 0, 1, 0 }, { 0, 0, 1, 1, 1 }, { 1, 1, 1, 0, 1 }, { 1, 0, 1, 1, 0 } }; Console.WriteLine(isbinaryMatrixPalindrome(mat)); } } // This code is contributed by ukasp.
Javascript
<script> // Javascript program for the above approach let N = 5; // Function to check if the matrix is // palindrome or not function isbinaryMatrixPalindrome(mat) { // Traverse the matrix and check if // top right and bottom left elements // have same value for (let i = 0; i < N - 1; i++) { for (let j = N - 1; j > i; j--) { // If top right element is not // equal to the bottom left // element return false if (mat[i][j] != mat[j][i]) { return "Np"; } } } return "Yes"; } // Driver Code let mat = [ [1, 0, 0, 1, 1], [0, 1, 0, 1, 0], [0, 0, 1, 1, 1], [1, 1, 1, 0, 1], [1, 0, 1, 1, 0], ]; document.write(isbinaryMatrixPalindrome(mat)); // This code is contributed by saurabh_jaiswal. </script>
Yes
Complejidad de Tiempo: O(N 2 )
Espacio Auxiliar: O(1)