Python Program to Interchange elements of first and last rows in matrix
Given a 4 x 4 matrix, we have to interchange the elements of first and last row and show the resulting matrix.
Examples :
Input : 3 4 5 0
2 6 1 2
2 7 1 2
2 1 1 2
Output : 2 1 1 2
2 6 1 2
2 7 1 2
3 4 5 0
Input : 9 7 5 1
2 3 4 1
5 6 6 5
1 2 3 1
Output : 1 2 3 1
2 3 4 1
5 6 6 5
9 7 5 1
Method 1:
The approach is very simple, we can simply swap the elements of first and last row of the matrix inorder to get the desired matrix as output.
Below is the implementation of the approach :
Python3
def interchangeFirstLast(mat, n, m):
rows = n
for i in range (n):
t = mat[ 0 ][i]
mat[ 0 ][i] = mat[rows - 1 ][i]
mat[rows - 1 ][i] = t
mat = [[ 8 , 9 , 7 , 6 ],
[ 4 , 7 , 6 , 5 ],
[ 3 , 2 , 1 , 8 ],
[ 9 , 9 , 7 , 7 ]]
n = 4
m = 4
interchangeFirstLast(mat, n, m)
for i in range (n):
for j in range (m):
print (mat[i][j], end = " " )
print ("")
|
Output
9 9 7 7
4 7 6 5
3 2 1 8
8 9 7 6
Time Complexity: O(N)
Auxiliary Space: O(1), as we are not using any extra space.
Method 2: Without any Loop
We can achieve this by swapping the first and last list of the list of lists(matrix) in python
Python3
mat = [[ 8 , 9 , 7 , 6 ],
[ 4 , 7 , 6 , 5 ],
[ 3 , 2 , 1 , 8 ],
[ 9 , 9 , 7 , 7 ]]
mat[ 0 ], mat[ - 1 ] = mat[ - 1 ], mat[ 0 ]
n = 4
m = 4
for i in range (n):
for j in range (m):
print (mat[i][j], end = " " )
print ()
|
Output
9 9 7 7
4 7 6 5
3 2 1 8
8 9 7 6
Time Complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix. This is because the time complexity of the code is dependent on the size of the matrix, and the code needs to traverse each element in the matrix.
Auxiliary Space: O(1), as the code only swaps the values of two rows and does not use any additional space.
Method 3 : Using pop(),insert() and append() methods
Python3
mat = [[ 8 , 9 , 7 , 6 ],
[ 4 , 7 , 6 , 5 ],
[ 3 , 2 , 1 , 8 ],
[ 9 , 9 , 7 , 7 ]]
x = mat[ - 1 ]
y = mat[ 0 ]
mat.pop()
mat.pop( 0 )
mat.insert( 0 , x)
mat.append(y)
n = 4
m = 4
for i in range (n):
for j in range (m):
print (mat[i][j], end = " " )
print ("")
|
Output
9 9 7 7
4 7 6 5
3 2 1 8
8 9 7 6
Method 4: Using XOR operation
Step-by-step approach:
- Initialize the matrix.
- XOR the first and last rows of the matrix using a loop and XOR operation.
- Print the resulting matrix.
Below is the implementation of the above approach:
Python3
mat = [[ 8 , 9 , 7 , 6 ],
[ 4 , 7 , 6 , 5 ],
[ 3 , 2 , 1 , 8 ],
[ 9 , 9 , 7 , 7 ]]
for i in range ( len (mat[ 0 ])):
mat[ 0 ][i] = mat[ 0 ][i] ^ mat[ - 1 ][i]
mat[ - 1 ][i] = mat[ 0 ][i] ^ mat[ - 1 ][i]
mat[ 0 ][i] = mat[ 0 ][i] ^ mat[ - 1 ][i]
for row in mat:
print ( ' ' .join( str (x) for x in row))
|
Output
9 9 7 7
4 7 6 5
3 2 1 8
8 9 7 6
Time complexity: O(n), where n is the number of elements in the matrix
Auxiliary space: O(1)
Please refer complete article on Interchange elements of first and last rows in matrix for more details!
Last Updated :
29 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...