Interchange Diagonal elements in Java
In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in a matrix, the algorithm to be followed and also a simple example program in java.
What is a Matrix in java?
Technically referring, a matrix is a rectangular, two-dimensional array that contains either integers, symbols, or statements as its fundamental elements. The elements in the matrix are arranged in rows and columns.
For an instance, m x n matrix refers to a matrix containing 'm' number of rows and 'n' number of columns. 'a [i][j]', which denotes that the element an is represented in the i-th row and j-th column, can be used to represent individual values in the matrix, which are referred to as elements.
Look at the example below to understand how a matrix actually looks like
Example:
A[][]= { 7, 8, 9,
4, 5, 6,
1, 2, 3}
The above example is a matrix or it can also be termed as two-dimensional array, with 3 rows and 3 columns.
Now let us understand what is meant by interchanging the diagonal elements.
Interchanging the diagonal elements in an array means to swap the elements contained in left diagonal to right diagonal and swap the elements contained in the right diagonal to the left diagonal.
Note: The interchanging can only happen when the matrix has the same number of rows and columns. Else interchanging the diagonal elements would not be possible. Even of possible, the interchanging cannot be very accurate and perfect.
Let us discuss an example that can be easily understood.
Consider a 4x4 matrix with
a[][]= { 7, 8, 9, 10,
4, 5, 6, 11,
1, 2, 3, 12
13, 14, 15,16 }
In the above matrix, interchanging of elements from left and right diagonal would happen as follows:
a[1][1] would swap with a[1][4]
a[2][2] would swap with a[2][3]
a[3][3] would swap with a[3][2]
a[4][4] would swap with a[4][1]
We notice that there is only difference in the row index between the left and right diagonals and there is no change in column index. If we pay close attention to the column indices, we can see a pattern where the column index mostly on left side is advancing by 1 and the column index on the right side is reducing by 1.
Therefore, the simple logic to interchange the diagonal elements is as follows
for(initialization; condition; increment/decrement)
{
Swap(a[i][i], a[i][n-i-1]); // where ‘n’ is the size of matrix
}
Algorithm
The below is the algorithm or step by step instruction that has to be followed when interchanging the diagonal elements of a given matrix.
- Initialize variables for size of the matrix.
- Seeking the user to set the matrix's rows and columns to zero
- Verify whether the total number of rows and columns is the same.
- The user is asked to initialise the matrix if the results are equal.
- Generate the matrix as it is.
- Change the diagonal units.
- Print the matrix with the diagonals switched.
- Print the identical statement if the columns and the rows are not equal.
Now let us understand how interchanging of diagonal elements work with the help of an example program.
File name: Swapelem.java
import java.util.Scanner;
public class Swapelem
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of rows and columns: ");
//Obtaining the input from the user
int n=s.nextInt();
//constructing a matrix of size n*n
int a[][] = new int [n][n];
//Matrix elements taken as input
System.out.println("Enter the elements of the matrix: ");
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
a[i][j]=s.nextInt();
}
}
//Displaying the actual matrix
System.out.println("\n Actual matrix: \n");
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println(" ");
}
//elements interchanging
for(int i=0; i<n; i++)
{
int temp = a[i][i];
a[i][i] = a[i][n-i-1];
a[i][n-i-1] = temp;
}
//prints the diagonal interchanged matrix
System.out.println("\n Matrix after the diagonals are interchanged: \n");
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println(" ");
}
}
}
Output:
Enter the number of rows and columns: 3
Enter the elements of the matrix:
1
2
3
4
5
6
7
8
9
Actual matrix:
1 2 3
4 5 6
7 8 9
Matrix after the diagonals are interchanged:
3 2 1
4 5 6
9 8 7
Time Complexity
The time complexity is O(n*n) because we have included nested loops for traversing through the matrix.
Space Complexity
The space complexity is O(1) because any extra space is not used in allocating the memory.