Introduction
Data structure and algorithms is one of the most important topics if you want to crack the interview at a top product-based company. Having proper knowledge of DSA requires one to solve many coding problems. So, in this blog, we will discuss a coding problem in which we have to swap the upper and lower diagonal elements of a matrix. We’ll be using a 2d array to define the matrix.
Recommended Topic, Array Implementation of Queue and Rabin Karp Algorithm
Problem Statement
Ninja has given you a square matrix of NxN dimension. You have to swap the lower diagonal elements with the upper diagonal elements and print the resultant matrix.
Sample Examples
Example 1
Input
Output
Explanation
Example 2
Input
Output
Explanation
Same as the previous example. We need to swap the lower diagonal elements with the upper diagonal elements.
Approach
As we have to swap the lower and upper diagonal elements. So, we’ll start traversing the array from the top left corner and we’ll keep track of the ith and jth location of the cell then as we proceed we’ll swap the element present at cell (i, j) with the element present at cell (j, i) until we reach at the end of the matrix.
Pseudocode
Algorithm SWAP_ELEMENTS(MAT)
for i <- 1 to n do
for j <- i+1 to n do
temp <- MAT[i][j]
MAT[i][j] = MAT[j][i]
MAT[j][i] = temp
end
end
Implementation in C++
// C++ program to swap upper diagonal elements with
// lower diagonal elements
#include <bits/stdc++.h>
using namespace std;
// function to swap elements in the matrix
void swapElements(vector<vector<int>> matrix){
// size of our matrix
int n = matrix.size();
// swapping elements of matrix
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// printing the result
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << matrix[i][j] << " ";
cout << endl;
}
}
// main function
int main() {
// getting the size of the matrix from user
int n;
cout << "Enter the size of the matrix: ";
cin >> n;
// input matrix;
vector<vector<int>> matrix(n, vector<int>(n));
// getting matrix elements from user
cout << "Enter matrix elements: \n";
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> matrix[i][j];
}
}
// Function call
swapElements(matrix);
return 0;
}
Implementation in Java
// Java program to swap upper diagonal elements with
// lower diagonal elements
import java.util.Scanner;
class CodingNinja {
// function to swap elements in the matrix
static void swapElements(int matrix[][]) {
// size of our matrix
int n = matrix.length;
// swapping elements of matrix
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// printing the result
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
System.out.print(matrix[i][j] + " ");
System.out.println("");
}
}
// main method
public static void main (String[] args) {
// getting matrix size from user
System.out.print("Enter the size of the matrix: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// getting matrix elements from user
int matrix[][] = new int[n][n];
System.out.println("Enter matrix elements: ");
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
matrix[i][j] = sc.nextInt();
}
}
// Function call
swapElements(matrix);
}
}
Input
Enter the size of the matrix: 3
Enter matrix elements:
1 2 3
6 5 4
7 9 8
Output
1 6 7
2 5 9
3 4 8
Time Complexity
The time complexity for the above program is O(N^2) as we need to traverse the matrix to swap the elements.
Space Complexity
We are not using any extra space. So, the space complexity of our program is O(1).