Interchange elements of first and last columns in matrix
Given a square matrix, the task is to interchange the elements of the first and last columns and show the resulting matrix
Examples:
Input: 8 9 7 6
4 7 6 5
3 2 1 8
9 9 7 7
Output: 6 9 7 8
5 7 6 4
8 2 1 3
7 9 7 9
Input: 9 7 5 1
2 3 4 1
5 6 6 5
1 2 3 1
Output: 1 7 5 9
1 3 4 2
5 6 6 5
1 2 3 1
To solve the problem follow the below idea:
Simply swap the elements of the first and last column of the matrix in order to get the desired matrix as output
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
#define n 4
void interchangeFirstLast( int m[][n])
{
for ( int i = 0; i < n; i++) {
int t = m[i][0];
m[i][0] = m[i][n - 1];
m[i][n - 1] = 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.*;
class GFG {
static int n = 4 ;
static void interchangeFirstLast( int m[][])
{
int cols = n;
for ( int i = 0 ; i < n; i++) {
int t = m[i][ 0 ];
m[i][ 0 ] = m[i][n - 1 ];
m[i][n - 1 ] = t;
}
}
public static void main(String[] args)
{
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 < n; i++) {
for ( int j = 0 ; j < n; j++)
System.out.print(m[i][j] + " " );
System.out.println();
}
}
}
|
Python 3
def interchangeFirstLast(mat, n, m):
rows = n
for i in range (n):
t = mat[i][ 0 ]
mat[i][ 0 ] = mat[i][n - 1 ]
mat[i][n - 1 ] = 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 {
static int n = 4;
static void interchangeFirstLast( int [, ] m)
{
for ( int i = 0; i < n; i++) {
int t = m[i, 0];
m[i, 0] = m[i, n - 1];
m[i, n - 1] = t;
}
}
public static void Main()
{
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 < n; i++) {
for ( int j = 0; j < n; j++)
Console.Write(m[i, j] + " " );
Console.WriteLine();
}
}
}
|
PHP
<?php
function interchangeFirstLast( $m , $n )
{
$cols = $n ;
for ( $i = 0; $i < $n ; $i ++)
{
$t = $m [ $i ][0];
$m [ $i ][0] = $m [ $i ][ $n - 1];
$m [ $i ][ $n - 1] = $t ;
}
return $m ;
}
$m = array ( array ( 8, 9, 7, 6 ),
array (4, 7, 6, 5 ),
array (3, 2, 1, 8 ),
array (9, 9, 7, 7 ));
$n = 4 ;
$m = interchangeFirstLast( $m , $n );
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
echo $m [ $i ][ $j ], " " ;
echo "\n" ;
}
?>
|
Javascript
<script>
n = 4
function interchangeFirstLast(m)
{
for ( var i = 0; i < n; i++)
{
var t = m[i][0];
m[i][0] = m[i][n - 1];
m[i][n - 1] = t;
}
}
m = [ [ 8, 9, 7, 6 ],
[ 4, 7, 6, 5 ],
[ 3, 2, 1, 8 ],
[ 9, 9, 7, 7 ] ];
interchangeFirstLast(m);
for ( var i = 0; i < n; i++)
{
for ( var j = 0; j < n; j++)
document.write( m[i][j] + " " );
document.write( "<br>" );
}
</script>
|
Output
6 9 7 8
5 7 6 4
8 2 1 3
7 9 7 9
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
20 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...