Program to Interchange Diagonals of Matrix
Given a square matrix of order n*n, you have to interchange the elements of both diagonals.
Examples :
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}
Expalnation : Idea behind interchanging diagonals of square matrix is simple. Iterate from 0 to n-1 and for each iteration you have to swap a[i][i] and a[i][n-i-1].

Time complexity : O(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) 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; } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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. |
chevron_right
filter_none
Output:
6 5 4 1 2 3 9 8 7
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Maximum size square sub-matrix with all 1s
- Print a given matrix in spiral form
- Search in a row wise and column wise sorted matrix
- A Boolean Matrix Question
- Matrix Chain Multiplication | DP-8
- Print unique rows in a given boolean matrix
- Inplace (Fixed space) M x N size matrix transpose | Updated
- Maximum sum rectangle in a 2D matrix | DP-27
- Zigzag (or diagonal) traversal of Matrix
- Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
- Print all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a mXn matrix
- Printing brackets in Matrix Chain Multiplication Problem
- Create a matrix with alternating rectangles of O and X
- Given an n x n square matrix, find sum of all sub-squares of size k x k
Improved By : vt_m



