Given a 4 x 4 matrix, we have to interchange the elements of the 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
Interchange elements of first and last rows in the matrix using the swap function:
To solve the problem follow the below idea:
The approach is very simple, we can simply swap the elements of the first and last row of the matrix in order to get the desired matrix as output
Below is the implementation of the approach:
C++
#include <iostream>
using namespace std;
#define n 4
void interchangeFirstLast( int m[][n])
{
int rows = n;
for ( int i = 0; i < n; i++) {
int t = m[0][i];
m[0][i] = m[rows - 1][i];
m[rows - 1][i] = t;
}
}
int main()
{
int m[n][n] = { { 8, 9, 7, 6 },
{ 4, 7, 6, 5 },
{ 3, 2, 1, 8 },
{ 9, 9, 7, 7 } };
interchangeFirstLast(m);
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++)
cout << m[i][j] << " " ;
cout << endl;
}
}
|
Java
import java.io.*;
public class Interchange {
static void interchangeFirstLast( int m[][])
{
int rows = m.length;
for ( int i = 0 ; i < m[ 0 ].length; i++) {
int t = m[ 0 ][i];
m[ 0 ][i] = m[rows - 1 ][i];
m[rows - 1 ][i] = t;
}
}
public static void main(String args[])
throws IOException
{
int m[][] = { { 8 , 9 , 7 , 6 },
{ 4 , 7 , 6 , 5 },
{ 3 , 2 , 1 , 8 },
{ 9 , 9 , 7 , 7 } };
interchangeFirstLast(m);
for ( int i = 0 ; i < m.length; i++) {
for ( int j = 0 ; j < m[ 0 ].length; j++)
System.out.print(m[i][j] + " " );
System.out.println();
}
}
}
|
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
if __name__ = = "__main__" :
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 ( "\n" )
|
C#
using System;
class GFG {
public static void interchangeFirstLast( int [][] m)
{
int rows = m.Length;
for ( int i = 0; i < m[0].Length; i++) {
int t = m[0][i];
m[0][i] = m[rows - 1][i];
m[rows - 1][i] = t;
}
}
public static void Main( string [] args)
{
int [][] m
= new int [][] { new int [] { 8, 9, 7, 6 },
new int [] { 4, 7, 6, 5 },
new int [] { 3, 2, 1, 8 },
new int [] { 9, 9, 7, 7 } };
interchangeFirstLast(m);
for ( int i = 0; i < m.Length; i++) {
for ( int j = 0; j < m[0].Length; j++) {
Console.Write(m[i][j] + " " );
}
Console.WriteLine();
}
}
}
|
PHP
<?php
$n = 4;
function interchangeFirstLast(& $m )
{
global $n ;
$rows = $n ;
for ( $i = 0; $i < $n ; $i ++)
{
$t = $m [0][ $i ];
$m [0][ $i ] = $m [ $rows - 1][ $i ];
$m [ $rows - 1][ $i ] = $t ;
}
}
$m = array ( array (8, 9, 7, 6),
array (4, 7, 6, 5),
array ( 3, 2, 1, 8),
array (9, 9, 7, 7));
interchangeFirstLast( $m );
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
echo $m [ $i ][ $j ] . " " ;
echo "\n" ;
}
?>
|
Javascript
<script>
function interchangeFirstLast(m)
{
let rows = m.length;
for (let i = 0; i < m[0].length; i++) {
let t = m[0][i];
m[0][i] = m[rows-1][i];
m[rows-1][i] = t;
}
}
let m = [[8, 9, 7, 6],
[4, 7, 6, 5],
[3, 2, 1, 8],
[9, 9, 7, 7]]
interchangeFirstLast(m);
for (let i = 0; i < m.length; i++) {
for (let j = 0; j < m[0].length; j++)
document.write(m[i][j] + " " );
document.write( "<br>" );
}
</script>
|
Output9 9 7 7
4 7 6 5
3 2 1 8
8 9 7 6
Time Complexity: O(N)
Auxiliary Space: O(1)