Remove first X rows and columns from a matrix
Last Updated :
15 Sep, 2022
Given an integer X and a square matrix mat[][], the task is to remove the first X rows and columns from the given matrix and print the updated matrix.
Examples:
Input: mat[][] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{8, 9, 4, 2},
{4, 8, 9, 2} },
X = 2
Output:
4 2
9 2
Input: mat[][] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9} },
X = 1
Output:
5 6
8 9
Approach: Print the elements of the matrix arr[i][j] for all i, j ? [X, N – 1] where N is the order of the given square matrix.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
const int MAX = 50;
void remove_row_col( int arr[][MAX], int n, int x)
{
for ( int i = x; i < n; i++) {
for ( int j = x; j < n; j++) {
cout << arr[i][j] << " " ;
}
cout << endl;
}
}
int main()
{
int n = 3;
int arr[][MAX] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int x = 1;
remove_row_col(arr, n, x);
}
|
Java
import java.io.*;
class GFG
{
static int MAX = 50 ;
static void remove_row_col( int arr[][], int n, int x)
{
for ( int i = x; i < n; i++)
{
for ( int j = x; j < n; j++)
{
System.out.print( arr[i][j] + " " );
}
System.out.println();
}
}
public static void main (String[] args)
{
int n = 3 ;
int arr[][] = { { 1 , 2 , 3 },
{ 4 , 5 , 6 },
{ 7 , 8 , 9 } };
int x = 1 ;
remove_row_col(arr, n, x);
}
}
|
Python3
def remove_row_col(arr, n, x):
for i in range (x, n):
for j in range (x, n):
print (arr[i][j], end = " " )
print ()
if __name__ = = "__main__" :
n = 3
MAX = 50
arr = [[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]]
x = 1
remove_row_col(arr, n, x)
|
C#
using System;
class GFG
{
static void remove_row_col( int [,]arr, int n, int x)
{
for ( int i = x; i < n; i++)
{
for ( int j = x; j < n; j++)
{
Console.Write(arr[i, j] + " " );
}
Console.WriteLine();
}
}
public static void Main()
{
int n = 3;
int [,]arr = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int x = 1;
remove_row_col(arr, n, x);
}
}
|
PHP
<?php
$MAX = 50;
function remove_row_col( $arr , $n , $x )
{
for ( $i = $x ; $i < $n ; $i ++)
{
for ( $j = $x ; $j < $n ; $j ++)
{
echo $arr [ $i ][ $j ] . " " ;
}
echo "\n" ;
}
}
$n = 3;
$arr = array ( array ( 1, 2, 3 ),
array ( 4, 5, 6 ),
array ( 7, 8, 9 ));
$x = 1;
remove_row_col( $arr , $n , $x );
?>
|
Javascript
<script>
let MAX = 50;
function remove_row_col(arr,n,x)
{
for (let i = x; i < n; i++)
{
for (let j = x; j < n; j++)
{
document.write( arr[i][j] + " " );
}
document.write( "<br>" );
}
}
let n = 3;
let arr = [[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]];
let x = 1;
remove_row_col(arr, n, x);
</script>
|
Complexity Analysis:
- Time Complexity: O(n^2), where n is an order of the given square matrix.
- Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...