Swap the elements of lower and upper triangular matrix
The problem to be addressed involves swapping the elements of the lower and upper triangular portions of a given matrix. A matrix is a twodimensional array with rows and columns, and the goal is to interchange the elements below the main diagonal with those above the main diagonal.
Problem Statement
Given a square matrix of size ROW x COL (where ROW = COL), the objective is to swap the elements below the main diagonal with the elements above the main diagonal and display the modified matrix.
Example
Consider the following matrix:
6 1 5 3
0 3 2 6
4 0 4 7
1 2 9 1
After swapping the elements of the lower and upper triangular portions, the modified matrix becomes:
6 0 4 1
1 3 0 2
5 2 4 9
3 6 7 1
Idea to Solve
To solve this problem, iterate through the matrix and swap each element below the main diagonal with the corresponding element above the main diagonal. As the matrix is square (ROW = COL), swapping elements with their corresponding elements will ensure that the lower and upper triangular portions are interchanged.
Pseudocode
Here's the pseudocode for the algorithm:
function swapElement(matrix):
if ROW != COL:
return // Not a square matrix
for i from 0 to ROW1:
for j from i+1 to COL1:
swap(matrix[i][j], matrix[j][i])
Algorithm Explanation
 Define a function
swapElement
that takes a 2D matrixmatrix
as its input.  Check if the matrix is square by comparing ROW and COL. If they are not equal, return from the function as the swapping is not possible.
 Iterate through the matrix using two nested loops. The outer loop iterates through the rows, and the inner loop
starts from
i+1
to iterate through the columns, effectively focusing on elements above the main diagonal.  Within the nested loops, swap the element at
matrix[i][j]
with the element atmatrix[j][i]
. This step swaps the lower triangular element with the corresponding upper triangular element.  After the loops complete, the matrix will have its lower and upper triangular portions swapped.
Code Solution

1) Swap diagonal elements of a matrix in java
2) Swap diagonal elements of a matrix in c++
3) Swap diagonal elements of a matrix in c
4) Swap diagonal elements of a matrix in c#
5) Swap diagonal elements of a matrix in php
6) Swap diagonal elements of a matrix in node js
7) Swap diagonal elements of a matrix in typescript
8) Swap diagonal elements of a matrix in python
9) Swap diagonal elements of a matrix in ruby
10) Swap diagonal elements of a matrix in scala
11) Swap diagonal elements of a matrix in swift
12) Swap diagonal elements of a matrix in kotlin
13) Swap diagonal elements of a matrix in vb.net
14) Swap diagonal elements of a matrix in golang
Time Complexity
The time complexity of this algorithm is O(ROW * COL), where ROW and COL are the number of rows and columns in the matrix, respectively. The algorithm iterates through each element of the matrix once to perform the swapping.
In conclusion, the provided C code snippet effectively solves the problem of swapping the elements of the lower and upper triangular portions of a square matrix. The pseudocode and algorithm provide a clear stepbystep approach, and the time complexity analysis elucidates the efficiency of the solution.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment