Javascript Program to Interchange Diagonals of Matrix
Last Updated :
10 Sep, 2024
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}
Explanation : Idea behind interchanging diagonals of a 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].

JavaScript
// 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) {
let output = "";
for (let j = 0; j < N; ++j)
output += array[i][j] + " "
console.log(output.trim());
}
}
// Driver Code
let array = [[4, 5, 6],
[1, 2, 3],
[7, 8, 9]];
interchangeDiagonals(array);
// This code is contributed by subham348.
Complexity Analysis:
- Time Complexity: O(N*N), as we are using nested loops for traversing the matrix.
- Auxiliary Space: O(1), as we are not using any extra space.
Please refer complete article on Program to Interchange Diagonals of Matrix for more details!
Similar Reads
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[][] =
5 min read
JavaScript Program to Print Matrix in Diagonal Pattern We have given the n*n size matrix and we need to print the elements of this matrix in the diagonal pattern using JavaScript language. Below are the examples for a better understanding of the problem statement. Examples:Input: mat[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}Output:1 2 4 7 5 3 6 8 9Expla
4 min read
Javascript Program for Mirror of matrix across diagonal Given a 2-D array of order N x N, print a matrix that is the mirror of the given tree across the diagonal. We need to print the result in a way: swap the values of the triangle above the diagonal with the values of the triangle below it like a mirror image swap. Print the 2-D array obtained in a mat
4 min read
Program to Interchange or Swap Columns in a Matrix Given a matrix having m rows and n columns, the task is to write a program to interchange any two Columns(i.e. column no. N and M given in the input) in the given matrix. Example: Input : N = 1, M = 2, mat[][] = {{2, 1, 4}, {1, 2, 3}, {3, 6, 2}}Output: mat[][] = {{1, 2, 4}, {2, 1, 3}, {6, 3, 2}} Inp
6 min read
Program to convert given Matrix to a Diagonal Matrix Given a N*N matrix. The task is to convert the matrix to a diagonal matrix. That is to change the values of the non-diagonal elements of a matrix to 0.Diagonal-Matrix: A matrix is called a Diagonal Matrix if all the non-diagonal elements of the matrix are zero.Examples: Input : mat[][] = {{ 2, 1, 7
8 min read