Dada una array mat[][] , la tarea es verificar si la ruta de orden de fila principal de la array es un palíndromo o no.
Ejemplos:
Entrada: mat[][] = {{1, 2, 3}, {4, 5, 4}, {3, 2, 1}}
Salida: SÍ
Explicación:
El recorrido de la array en el orden principal de las filas es –
1 => 2 => 3 => 4 => 5 => 4 => 3 => 2 => 1
El recorrido inverso de la array en orden mayor por filas es –
1 => 2 => 3 => 4 => 5 => 4 => 3 => 2 => 1
Dado que el recorrido inverso y directo de la array es el mismo.
Por lo tanto, es un palíndromo.Entrada: mat[][] = {{1, 2}, {2, 3}}
Salida: NO
Enfoque: La idea es atravesar la media array y verificar que sus medios opuestos sean iguales o no al mismo tiempo. Eso es para el índice de i y j, verifique eso y . Si para algún índice hay una discrepancia, escriba NO.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to check if // row-major order traversal of // matrix is palindrome or not #include <bits/stdc++.h> using namespace std; // Function to check if row-major order // traversal of the matrix is is palindrome bool isPal(int a[3][3], int n, int m) { // Loop to check if the matrix is // matrix is palindrome or not for (int i = 0; i < n / 2; i++) { for (int j = 0; j < m - 1; j++) { if (a[i][j] != a[n - 1 - i][m - 1 - j]) return false; } } return true; } // Driver Code int main() { int n = 3, m = 3; int a[3][3] = { { 1, 2, 3 }, { 4, 5, 4 }, { 3, 2, 1 } }; if (isPal(a, n, m)) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
Java
// Java implementation to check if // row-major order traversal of // matrix is palindrome or not import java.util.*; class GFG{ // Function to check if row-major order // traversal of the matrix is is palindrome static boolean isPal(int a[][], int n, int m) { // Loop to check if the matrix is // matrix is palindrome or not for(int i = 0; i < n / 2; i++) { for(int j = 0; j < m - 1; j++) { if (a[i][j] != a[n - 1 - i][m - 1 - j]) return false; } } return true; } // Driver Code public static void main(String[] args) { int n = 3, m = 3; int a[][] = { { 1, 2, 3 }, { 4, 5, 4 }, { 3, 2, 1 } }; if (isPal(a, n, m)) { System.out.print("YES" + "\n"); } else { System.out.print("NO" + "\n"); } } } // This code is contributed by gauravrajput1
Python3
# Python3 implementation to check if # row-major order traversal of # matrix is palindrome or not # Function to check if row-major order # traversal of the matrix is is palindrome def isPal(a, n, m): # Loop to check if the matrix is # matrix is palindrome or not for i in range(0, n // 2): for j in range(0, m - 1): if (a[i][j] != a[n - 1 - i][m - 1 - j]): return False; return True; # Driver Code if __name__ == '__main__': n = 3; m = 3; a = [[1, 2, 3], [4, 5, 4], [3, 2, 1]]; if (isPal(a, n, m)): print("YES"); else: print("NO"); # This code is contributed by Princi Singh
C#
// C# implementation to check if // row-major order traversal of // matrix is palindrome or not using System; class GFG{ // Function to check if row-major order // traversal of the matrix is is palindrome static bool isPal(int[,]a, int n, int m) { // Loop to check if the matrix is // matrix is palindrome or not for(int i = 0; i < n / 2; i++) { for(int j = 0; j < m - 1; j++) { if (a[i, j] != a[n - 1 - i, m - 1 - j]) return false; } } return true; } // Driver Code public static void Main(String[] args) { int n = 3, m = 3; int[,]a = { { 1, 2, 3 }, { 4, 5, 4 }, { 3, 2, 1 } }; if (isPal(a, n, m)) { Console.Write("YES" + "\n"); } else { Console.Write("NO" + "\n"); } } } // This code is contributed by gauravrajput1
Javascript
<script> // JavaScript implementation to check if // row-major order traversal of // matrix is palindrome or not // Function to check if row-major order // traversal of the matrix is is palindrome function isPal(a, n, m) { // Loop to check if the matrix is // matrix is palindrome or not for(let i = 0; i < n / 2; i++) { for(let j = 0; j < m - 1; j++) { if (a[i][j] != a[n - 1 - i][m - 1 - j]) return false; } } return true; } // Driver Code let n = 3, m = 3; let a = [ [ 1, 2, 3 ], [ 4, 5, 4 ], [ 3, 2, 1 ] ]; if (isPal(a, n, m)) { document.write("YES" + "\n"); } else { document.write("NO" + "\n"); } // This code is contributed by susmitakundugoaldanga </script>
YES
Publicación traducida automáticamente
Artículo escrito por grand_master y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA