VOOZH about

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

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 19 Jul, 2022

Here, we will see how to interchange the elements of first and last in a matrix across rows. Below are the examples:

Input: 6 3 1
           4 5 2
           2 4 9
Output: 2 4 9
              4 5 2
              6 3 1

Input: 1 3 5 
           4 5 2 
           2 2 4 
Output: 2 2 4 
              4 5 2 
              1 3 5

Approach: The approach is very simple, swap the elements of the first and last row of the matrix in order to get the desired matrix as output.

Below is the C program to interchange the elements of first and last in a matrix across rows:


Output
Input Matrix: 
6 3 1 
4 5 2 
2 4 9 

Output Matrix: 
2 4 9 
4 5 2 
6 3 1 

Time Complexity: O(n2)
Auxiliary Space: O(1)

Comment
Article Tags: