![]() |
VOOZH | about |
Here, we will see how to interchange the elements of the first and last element/entries in a matrix across columns; with an approach of swapping.
Input:
9 7 5 2 3 4 5 2 6
Output:
5 7 9 4 3 2 6 2 5
The approach is very simple for this program, we can simply swap the elements of the first and last columns of the matrix in order to get the desired matrix as output.
Example:
Input Matrix: 2 4 6 8 2 3 1 9 4 Output Matrix: 6 4 2 3 2 8 4 9 1
Time Complexity: O(n)
Auxiliary Space: O(1)