Dada una array cuadrada de orden n*n, hay que intercambiar los elementos de ambas diagonales.
Ejemplos:
Input : matrix[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : matrix[][] = {3, 2, 1, 4, 5, 6, 9, 8, 7} Input : matrix[][] = {4, 2, 3, 1, 5, 7, 6, 8, 9, 11, 10, 12, 16, 14, 15, 13} Output : matrix[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 11, 14, 15, 16}
Explicación: la idea detrás del intercambio de diagonales de una array cuadrada es simple. Itere de 0 a n-1 y para cada iteración debe intercambiar a[i][i] y a[i][ni-1].
Implementación:
C++
// C++ program to interchange // the diagonals of matrix #include<bits/stdc++.h> using namespace std; #define N 3 // Function to interchange diagonals void interchangeDiagonals(int array[][N]) { // swap elements of diagonal for (int i = 0; i < N; ++i) if (i != N / 2) swap(array[i][i], array[i][N - i - 1]); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) cout<<" "<< array[i][j]; cout<<endl; } } // Driver Code int main() { int array[N][N] = {4, 5, 6, 1, 2, 3, 7, 8, 9}; interchangeDiagonals(array); return 0; } // This code is contributed by noob2000.
C
// C program to interchange // the diagonals of matrix #include<bits/stdc++.h> using namespace std; #define N 3 // Function to interchange diagonals void interchangeDiagonals(int array[][N]) { // swap elements of diagonal for (int i = 0; i < N; ++i) if (i != N / 2) swap(array[i][i], array[i][N - i - 1]); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) printf(" %d", array[i][j]); printf("\n"); } } // Driver Code int main() { int array[N][N] = {4, 5, 6, 1, 2, 3, 7, 8, 9}; interchangeDiagonals(array); return 0; }
Java
// Java program to interchange // the diagonals of matrix import java.io.*; class GFG { public static int N = 3; // Function to interchange diagonals static void interchangeDiagonals(int array[][]) { // swap elements of diagonal for (int i = 0; i < N; ++i) if (i != N / 2) { int temp = array[i][i]; array[i][i] = array[i][N - i - 1]; array[i][N - i - 1] = temp; } for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) System.out.print(array[i][j]+" "); System.out.println(); } } // Driver Code public static void main (String[] args) { int array[][] = { {4, 5, 6}, {1, 2, 3}, {7, 8, 9} }; interchangeDiagonals(array); } } // This code is contributed by Pramod Kumar
Python3
# Python program to interchange # the diagonals of matrix N = 3; # Function to interchange diagonals def interchangeDiagonals(array): # swap elements of diagonal for i in range(N): if (i != N / 2): temp = array[i][i]; array[i][i] = array[i][N - i - 1]; array[i][N - i - 1] = temp; for i in range(N): for j in range(N): print(array[i][j], end = " "); print(); # Driver Code if __name__ == '__main__': array = [ 4, 5, 6 ],[ 1, 2, 3 ],[ 7, 8, 9 ]; interchangeDiagonals(array); # This code is contributed by Rajput-Ji
C#
// C# program to interchange // the diagonals of matrix using System; class GFG { public static int N = 3; // Function to interchange diagonals static void interchangeDiagonals(int [,]array) { // swap elements of diagonal for (int i = 0; i < N; ++i) if (i != N / 2) { int temp = array[i, i]; array[i, i] = array[i, N - i - 1]; array[i, N - i - 1] = temp; } for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) Console.Write(array[i, j]+" "); Console.WriteLine(); } } // Driver Code public static void Main () { int [,]array = { {4, 5, 6}, {1, 2, 3}, {7, 8, 9} }; interchangeDiagonals(array); } } // This code is contributed by vt_m.
Javascript
<script> // Javascript program to interchange // the diagonals of matrix let N = 3; // Function to interchange diagonals function interchangeDiagonals(array) { // swap elements of diagonal for (let i = 0; i < N; ++i) if (i != parseInt(N / 2)) { let temp = array[i][i]; array[i][i] = array[i][N - i - 1]; array[i][N - i - 1] = temp; } for (let i = 0; i < N; ++i) { for (let j = 0; j < N; ++j) document.write(" " + array[i][j]); document.write("<br>"); } } // Driver Code let array = [[4, 5, 6], [1, 2, 3], [7, 8, 9]]; interchangeDiagonals(array); // This code is contributed by subham348. </script>
6 5 4 1 2 3 9 8 7
Complejidad de tiempo: O (N * N), ya que estamos usando bucles anidados para atravesar la array.
Espacio auxiliar: O(1), ya que no estamos utilizando ningún espacio adicional.
Este artículo es una contribución de Shivam Pradhan (anuj_charm) . 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