Interchange elements of first and last rows in matrix
Last Updated :
20 Feb, 2023
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++
// C++ code to swap the element of first
// and last row and display the result
#include <iostream>
using namespace std;
#define n 4
void interchangeFirstLast(int m[][n])
{
int rows = n;
// swapping of element between first
// and last rows
for (int i = 0; i < n; i++) {
int t = m[0][i];
m[0][i] = m[rows - 1][i];
m[rows - 1][i] = t;
}
}
// Driver code
int main()
{
// input in the array
int m[n][n] = { { 8, 9, 7, 6 },
{ 4, 7, 6, 5 },
{ 3, 2, 1, 8 },
{ 9, 9, 7, 7 } };
// Function call
interchangeFirstLast(m);
// printing the interchanged matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << m[i][j] << " ";
cout << endl;
}
}
// This code is contributed by Anant Agarwal.
Java
// Java code to swap the element of first
// and last row and display the result
import java.io.*;
public class Interchange {
static void interchangeFirstLast(int m[][])
{
int rows = m.length;
// swapping of element between first
// and last rows
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;
}
}
// Driver code
public static void main(String args[])
throws IOException
{
// input in the array
int m[][] = { { 8, 9, 7, 6 },
{ 4, 7, 6, 5 },
{ 3, 2, 1, 8 },
{ 9, 9, 7, 7 } };
// Function call
interchangeFirstLast(m);
// printing the interchanged matrix
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
# Python3 code to swap the element
# of first and last row and display
# the result
def interchangeFirstLast(mat, n, m):
rows = n
# swapping of element between
# first and last rows
for i in range(n):
t = mat[0][i]
mat[0][i] = mat[rows-1][i]
mat[rows-1][i] = t
# Driver code
if __name__ == "__main__":
mat = [[8, 9, 7, 6],
[4, 7, 6, 5],
[3, 2, 1, 8],
[9, 9, 7, 7]]
n = 4
m = 4
# Function call
interchangeFirstLast(mat, n, m)
# printing the interchanged matrix
for i in range(n):
for j in range(m):
print(mat[i][j], end=" ")
print("\n")
# This code is contributed by Shrikant13.
C#
// C# code to swap the element of first
// and last row and display the result
using System;
class GFG {
public static void interchangeFirstLast(int[][] m)
{
int rows = m.Length;
// swapping of element between first
// and last rows
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;
}
}
// Driver code
public static void Main(string[] args)
{
// input in the array
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 } };
// Function call
interchangeFirstLast(m);
// printing the interchanged matrix
for (int i = 0; i < m.Length; i++) {
for (int j = 0; j < m[0].Length; j++) {
Console.Write(m[i][j] + " ");
}
Console.WriteLine();
}
}
}
// This code is contributed by Shrikant13
PHP
<?php
// PHP code to swap the element of first
// and last row and display the result
$n = 4;
function interchangeFirstLast(&$m)
{
global $n;
$rows = $n;
// swapping of element between first
// and last rows
for ($i = 0; $i < $n; $i++)
{
$t = $m[0][$i];
$m[0][$i] = $m[$rows - 1][$i];
$m[$rows - 1][$i] = $t;
}
}
// Driver code
// input in the array
$m = array(array(8, 9, 7, 6),
array(4, 7, 6, 5),
array( 3, 2, 1, 8),
array(9, 9, 7, 7));
// Function call
interchangeFirstLast($m);
// printing the interchanged matrix
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $n; $j++)
echo $m[$i][$j] . " ";
echo "\n";
}
?>
JavaScript
<script>
// Javascript code to swap the element of first
// and last row and display the result
function interchangeFirstLast(m)
{
let rows = m.length;
// swapping of element between first
// and last rows
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;
}
}
// Driver code
// input in the array
let m = [[8, 9, 7, 6],
[4, 7, 6, 5],
[3, 2, 1, 8],
[9, 9, 7, 7]]
interchangeFirstLast(m);
// printing the interchanged matrix
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>");
}
// This code is contributed by avanitrachhadiya2155
</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)
Similar Reads
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 Recommend
6 min read
Program to Interchange or Swap Columns in a Matrix Given a matrix having m rows and n columns, the task is to write a program to interchange any two Columns(i.e. column no. N and M given in the input) in the given matrix. Example: Input : N = 1, M = 2, mat[][] = {{2, 1, 4}, {1, 2, 3}, {3, 6, 2}}Output: mat[][] = {{1, 2, 4}, {2, 1, 3}, {6, 3, 2}} Inp
6 min read
Find the original matrix when largest element in a row and a column are given Given two arrays A[] and B[] of N and M integers respectively. Also given is a N X M binary matrix where 1 indicates that there was a positive integer in the original matrix and 0 indicates that the position is filled with 0 in the original matrix. The task is to form back the original matrix such t
6 min read
Boundary elements of a Matrix Given a matrix mat[][] of size n*m, the task is to traverse the boundary elements in clockwise order, starting from the top-left element.Examples: Input: mat[][] = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12] ]Output : 1 2 3 4 8 12 11 10 9 5 Input: mat[][] = [ [1, 2], [3, 4]]Output: 1 2 4 3Approach: T
10 min read
Reverse the rows and columns of a matrix alternatively Given a matrix arr[][] of size M*N, where M is the number of rows and N is the number of columns. The task is to reverse the rows and columns of the matrix alternatively i.e start with reversing the 1st row, then the 2nd column, and so on. Examples: Input: arr[][] = { {3, 4, 1, 8}, {11, 23, 43, 21},
7 min read