VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-to-interchange-elements-of-first-and-last-in-a-matrix-across-columns/

⇱ C Program to Interchange Elements of First and Last in a Matrix Across Columns - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program to Interchange Elements of First and Last in a Matrix Across Columns

Last Updated : 1 Aug, 2022


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

Approach: Swapping

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:


Output
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)

Comment
Article Tags: