Given an NxN matrix and an integer X. The task is to remove X rows and X columns from the NxN matrix.
- Remove the first X rows and columns from the matrix.
Examples:
Input: n = 4,
arr[][] = {{20, 2, 10, 16},
{20, 17, 11, 6},
{14, 16, 1, 3},
{10, 2, 17, 4}},
x = 2
Output:
1 3
17 4
Explanation:
Here the value of X is 2.
So remove the first 2 rows
and the first 2 columns from the matrix.
Hence the output is
1 3
17 4
Simple Approach:
- Skip the first x rows and columns and print the remaining elements in the matrix.
- Start from the xth row and print till the n-1th row.
- Start from the xth column and print till the n-1th column.
Implementation:
C++
#include <iostream>
using namespace std;
void remove_X_Rows_and_Columns( int * a, int n, int x)
{
cout << "\nRemoving First " << x
<< " rows and columns:\n" ;
for ( int i = x; i < n; i++) {
for ( int j = x; j < n; j++) {
cout << *((a + i * n) + j) << " " ;
}
cout << endl;
}
}
void printMatrix( int * a, int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
cout << *((a + i * n) + j) << " " ;
}
cout << endl;
}
}
int main()
{
int n = 4;
int a[n][n];
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
a[i][j] = (i * 10 + j);
}
}
cout << "Original Matrix:\n" ;
printMatrix(( int *)a, n);
int x = 2;
remove_X_Rows_and_Columns(( int *)a, n, x);
return 0;
}
|
Java
class GFG{
static void remove_X_Rows_and_Columns( int a[][],
int n, int x)
{
System.out.print( "\nRemoving First " + x +
" rows and columns:\n" );
for ( int i = x; i < n; i++)
{
for ( int j = x; j < n; j++)
{
System.out.print(a[i][j] + " " );
}
System.out.println();
}
}
static void printMatrix( int a[][], int n)
{
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
System.out.print(a[ i][j] + " " );
}
System.out.println();
}
}
public static void main(String [] args)
{
int n = 4 ;
int a[][] = new int [n][n];
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
a[i][j] = (i * 10 + j);
}
}
System.out.println( "Original Matrix:" );
printMatrix(a, n);
int x = 2 ;
remove_X_Rows_and_Columns(a, n, x);
}
}
|
Python3
def remove_X_Rows_and_Columns(a, n, x):
print ( "Removing First " , x,
" rows and columns:" )
for i in range (x, n):
for j in range (x, n):
print (a[i][j], end = " " )
print ()
def printMatrix(a, n):
for i in range (n):
for j in range (n):
print (a[i][j], end = " " )
print ()
if __name__ = = '__main__' :
n = 4
a = [[ 0 for i in range (n)]
for i in range (n)]
for i in range (n):
for j in range (n):
a[i][j] = (i * 10 + j)
print ( "Original Matrix:" )
printMatrix(a, n)
x = 2
remove_X_Rows_and_Columns(a, n, x)
|
C#
using System;
class GFG{
static void remove_X_Rows_and_Columns( int [,] a,
int n, int x)
{
Console.WriteLine( "\nRemoving First " + x +
" rows and columns:\n" );
for ( int i = x; i < n; i++)
{
for ( int j = x; j < n; j++)
{
Console.Write(a[i, j] + " " );
}
Console.WriteLine();
}
}
static void printMatrix( int [,] a, int n)
{
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
Console.Write(a[i, j] + " " );
}
Console.WriteLine();
}
}
static public void Main()
{
int n = 4;
int [,] a = new int [n, n];
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
a[i,j] = (i * 10 + j);
}
}
Console.WriteLine( "Original Matrix:" );
printMatrix(a, n);
int x = 2;
remove_X_Rows_and_Columns(a, n, x);
}
}
|
PHP
<?php
function remove_X_Rows_and_Columns( $a , $n , $x )
{
echo "\nRemoving First " , $x ,
" rows and columns:\n" ;
for ( $i = $x ; $i < $n ; $i ++)
{
for ( $j = $x ; $j < $n ; $j ++)
{
echo $a [ $i ][ $j ], " " ;
}
echo "\n" ;
}
}
function printMatrix( $a , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
echo $a [ $i ][ $j ], " " ;
}
echo "\n" ;
}
}
$n = 4;
$a = array ( array ());
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
$a [ $i ][ $j ] = $i * 10 + $j ;
}
}
echo "Original Matrix:\n" ;
printMatrix( $a , $n );
$x = 2;
remove_X_Rows_and_Columns( $a , $n , $x );
?>
|
Javascript
<script>
function remove_X_Rows_and_Columns(a , n , x) {
document.write( "<br/>Removing First " + x + " rows and columns:<br/>" );
for (i = x; i < n; i++) {
for (j = x; j < n; j++) {
document.write(a[i][j] + " " );
}
document.write( "<br/>" );
}
}
function printMatrix(a , n) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
document.write(a[i][j] + " " );
}
document.write( "<br/>" );
}
}
var n = 4;
var a = Array(n);
for (i = 0; i < n; i++) {
a[i] = Array(n).fill(0);
for (j = 0; j < n; j++) {
a[i][j] = (i * 10 + j);
}
}
document.write( "Original Matrix:" );
printMatrix(a, n);
var x = 2;
remove_X_Rows_and_Columns(a, n, x);
</script>
|
Output
Original Matrix:
0 1 2 3
10 11 12 13
20 21 22 23
30 31 32 33
Removing First 2 rows and columns:
22 23
32 33
- Remove the last X rows and columns from the matrix.
Examples:
Input: n = 4,
arr[][] = {{20, 2, 10, 16},
{20, 17, 11, 6},
{14, 16, 1, 3},
{10, 2, 17, 4}},
x = 2
Output:
20 2
20 17
Explanation:
Here the value of X is 2.
So remove the last 2 rows and
the last 2 columns from the matrix.
Hence the output is
20 2
20 17
Simple Approach
- Skip the last x rows and columns and print the remaining elements in the matrix.
- Start from the 0th row and print till the n-xth row.
- Start from the 0th column and print till the n-xth column.
Implementation:
C++
#include <iostream>
using namespace std;
void remove_X_Rows_and_Columns( int * a, int n, int x)
{
cout << "\nRemoving Last " << x
<< " rows and columns:\n" ;
for ( int i = 0; i < n - x; i++) {
for ( int j = 0; j < n - x; j++) {
cout << *((a + i * n) + j) << " " ;
}
cout << endl;
}
}
void printMatrix( int * a, int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
cout << *((a + i * n) + j) << " " ;
}
cout << endl;
}
}
int main()
{
int n = 4;
int a[n][n];
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
a[i][j] = (i * 10 + j);
}
}
cout << "Original Matrix:\n" ;
printMatrix(( int *)a, n);
int x = 2;
remove_X_Rows_and_Columns(( int *)a, n, x);
return 0;
}
|
Java
import java.io.*;
class GFG{
public static void remove_X_Rows_and_Columns(
int [][] a, int n, int x)
{
System.out.println( "\nRemoving Last " + x +
" rows and columns:\n" );
for ( int i = 0 ; i < n - x; i++)
{
for ( int j = 0 ; j < n - x; j++)
{
System.out.print(a[i][j] + " " );
}
System.out.println();
}
}
public static void printMatrix( int [][] a, int n)
{
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
System.out.print(a[i][j] + " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
int n = 4 ;
int [][] a = new int [n][n];
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
a[i][j] = (i * 10 + j);
}
}
System.out.println( "Original Matrix:" );
printMatrix(a, n);
int x = 2 ;
remove_X_Rows_and_Columns(a, n, x);
}
}
|
Python3
def remove_X_Rows_and_Columns(a, n, x):
print ( "\nRemoving Last" , x, "rows and columns:" )
for i in range (n - x):
for j in range (n - x):
print (a[i][j], end = " " )
print ()
def printMatrix(a, n):
for i in range (n):
for j in range (n):
print (a[i][j], end = " " )
print ()
n = 4
a = [[ 0 for i in range (n)] for j in range (n)]
for i in range (n):
for j in range (n):
a[i][j] = (i * 10 + j)
print ( "Original Matrix:" )
printMatrix(a, n)
x = 2
remove_X_Rows_and_Columns(a, n, x)
|
C#
using System;
public class GFG{
public static void remove_X_Rows_and_Columns(
int [,] a, int n, int x)
{
Console.WriteLine( "\nRemoving Last " + x +
" rows and columns:\n" );
for ( int i = 0; i < n - x; i++)
{
for ( int j = 0; j < n - x; j++)
{
Console.Write(a[i,j] + " " );
}
Console.WriteLine();
}
}
public static void printMatrix( int [,] a, int n)
{
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
Console.Write(a[i,j] + " " );
}
Console.WriteLine();
}
}
static public void Main (){
int n = 4;
int [,] a = new int [n,n];
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
a[i,j] = (i * 10 + j);
}
}
Console.WriteLine( "Original Matrix:" );
printMatrix(a, n);
int x = 2;
remove_X_Rows_and_Columns(a, n, x);
}
}
|
Javascript
<script>
function remove_X_Rows_and_Columns(a, n, x)
{
document.write( "<br>Removing Last" + x+ "rows and columns:<br>" );
for (let i = 0; i < n - x; i++) {
for (let j = 0; j < n - x; j++) {
document.write(a[i][j]+ " " );
}
document.write( "<br>" )
}
}
function printMatrix(a,n)
{
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
document.write(a[i][j]+ " " );
}
document.write( "<br>" )
}
}
let n = 4;
let a = new Array(n);
for (let i = 0; i < n; i++) {
a[i]= new Array(n);
for (let j = 0; j < n; j++) {
a[i][j] = (i * 10 + j);
}
}
document.write( "Original Matrix:<br>" );
printMatrix(a, n);
let x = 2;
remove_X_Rows_and_Columns(a, n, x);
</script>
|
Output
Original Matrix:
0 1 2 3
10 11 12 13
20 21 22 23
30 31 32 33
Removing Last 2 rows and columns:
0 1
10 11
- Remove the first X rows and last X columns from the matrix.
Examples:
Input: n = 4,
arr[][] = {{20, 2, 10, 16},
{20, 17, 11, 6},
{14, 16, 1, 3},
{10, 2, 17, 4}},
x = 2
Output:
14 16
10 2
Explanation:
Here the value of X is 2.
So remove the first 2 rows
and the last 2 columns from the matrix.
Hence the output is
14 16
10 2
Simple Approach
- Skip the first x rows and last x columns and print the remaining elements in the matrix.
- Start from the xth row and print till the n-1th row.
- Start from the 0th column and print till the n-xth column.
Implementation:
C++
#include <iostream>
using namespace std;
void remove_X_Rows_and_Columns( int * a, int n, int x)
{
cout << "\nRemoving First " << x
<< " rows and Last " << x
<< " columns:\n" ;
for ( int i = x; i < n; i++) {
for ( int j = 0; j < n - x; j++) {
cout << *((a + i * n) + j) << " " ;
}
cout << endl;
}
}
void printMatrix( int * a, int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
cout << *((a + i * n) + j) << " " ;
}
cout << endl;
}
}
int main()
{
int n = 4;
int a[n][n];
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
a[i][j] = (i * 10 + j);
}
}
cout << "Original Matrix:\n" ;
printMatrix(( int *)a, n);
int x = 2;
remove_X_Rows_and_Columns(( int *)a, n, x);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void remove_X_Rows_and_Columns( int [][] a, int n,
int x)
{
System.out.print( "\nRemoving First " + x +
" rows and Last " + x +
" columns:\n" );
for ( int i = x; i < n; i++)
{
for ( int j = 0 ; j < n - x; j++)
{
System.out.print(a[i][j]+ " " );
}
System.out.println();
}
}
static void printMatrix( int [][] a, int n)
{
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
System.out.print(a[i][j] + " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
int n = 4 ;
int [][]a = new int [n][n];
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
a[i][j] = (i * 10 + j);
}
}
System.out.print( "Original Matrix:\n" );
printMatrix(a, n);
int x = 2 ;
remove_X_Rows_and_Columns(a, n, x);
}
}
|
Python3
def remove_X_Rows_and_Columns(a, n, x):
print ( "\nRemoving First " , x , " rows and Last " , x , " columns:" );
for i in range (x, n):
for j in range (n - x):
print (a[i][j], end = " " );
print ();
def printMatrix(a, n):
for i in range (n):
for j in range (n):
print (a[i][j], end = " " );
print ();
if __name__ = = '__main__' :
n = 4 ;
a = [[ 0 for i in range (n)] for j in range (n)]
for i in range (n):
for j in range (n):
a[i][j] = (i * 10 + j);
print ( "Original Matrix:" );
printMatrix(a, n);
x = 2 ;
remove_X_Rows_and_Columns(a, n, x);
|
C#
using System;
public class GFG{
static void remove_X_Rows_and_Columns( int [,] a, int n,
int x)
{
Console.Write( "\nRemoving First " + x +
" rows and Last " + x +
" columns:\n" );
for ( int i = x; i < n; i++)
{
for ( int j = 0; j < n - x; j++)
{
Console.Write(a[i,j]+ " " );
}
Console.WriteLine();
}
}
static void printMatrix( int [,] a, int n)
{
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
Console.Write(a[i,j] + " " );
}
Console.WriteLine();
}
}
public static void Main(String[] args)
{
int n = 4;
int [,]a = new int [n,n];
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
a[i,j] = (i * 10 + j);
}
}
Console.Write( "Original Matrix:\n" );
printMatrix(a, n);
int x = 2;
remove_X_Rows_and_Columns(a, n, x);
}
}
|
Javascript
<script>
function remove_X_Rows_and_Columns(a , n,x)
{
document.write( "<br>Removing First " + x +
" rows and Last " + x +
" columns:<br>" );
for ( var i = x; i < n; i++)
{
for ( var j = 0; j < n - x; j++)
{
document.write(a[i][j]+ " " );
}
document.write( '<br>' );
}
}
function printMatrix(a , n)
{
for ( var i = 0; i < n; i++)
{
for ( var j = 0; j < n; j++)
{
document.write(a[i][j] + " " );
}
document.write( '<br>' );
}
}
var n = 4;
var a = Array(n).fill(0).map(x => Array(n).fill(0));
for ( var i = 0; i < n; i++)
{
for ( var j = 0; j < n; j++)
{
a[i][j] = (i * 10 + j);
}
}
document.write( "Original Matrix:<br>" );
printMatrix(a, n);
var x = 2;
remove_X_Rows_and_Columns(a, n, x);
</script>
|
Output
Original Matrix:
0 1 2 3
10 11 12 13
20 21 22 23
30 31 32 33
Removing First 2 rows and Last 2 columns:
20 21
30 31
- Remove the last X rows and first x columns from the matrix.
Examples:
Input: n = 4,
arr[][] = {{20, 2, 10, 16},
{20, 17, 11, 6},
{14, 16, 1, 3},
{10, 2, 17, 4}},
x = 2
Output:
10 16
11 6
Explanation:
Here the value of X is 2.
So remove the last 2 rows and
the first 2 columns from the matrix.
Hence the output is
10 16
11 6
Simple Approach
- Skip the last x rows and first x columns and print the remaining elements in the matrix.
- Start from the 0th row and print till the n-xth row.
- Start from the xth column and print till the n-1th column.
Implementation:
C++
#include <iostream>
using namespace std;
void remove_X_Rows_and_Columns( int * a, int n, int x)
{
cout << "\nRemoving Last " << x
<< " rows and First " << x
<< " columns:\n" ;
for ( int i = 0; i < n - x; i++) {
for ( int j = x; j < n; j++) {
cout << *((a + i * n) + j) << " " ;
}
cout << endl;
}
}
void printMatrix( int * a, int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
cout << *((a + i * n) + j) << " " ;
}
cout << endl;
}
}
int main()
{
int n = 4;
int a[n][n];
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
a[i][j] = (i * 10 + j);
}
}
cout << "Original Matrix:\n" ;
printMatrix(( int *)a, n);
int x = 2;
remove_X_Rows_and_Columns(( int *)a, n, x);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void remove_X_Rows_and_Columns( int [][] a, int n, int x)
{
System.out.print( "\nRemoving Last " + x
+ " rows and First " + x
+ " columns:\n" );
for ( int i = 0 ; i < n - x; i++) {
for ( int j = x; j < n; j++) {
System.out.print(a[i][j]+ " " );
}
System.out.println();
}
}
static void printMatrix( int [][] a, int n)
{
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
System.out.print(a[i][j]+ " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
int n = 4 ;
int [][]a= new int [n][n];
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
a[i][j] = (i * 10 + j);
}
}
System.out.print( "Original Matrix:\n" );
printMatrix(a, n);
int x = 2 ;
remove_X_Rows_and_Columns(a, n, x);
}
}
|
Python3
def remove_X_Rows_and_Columns(a, n, x):
print ( "\nRemoving Last " , x ,
" rows and First " , x,
" columns:" )
for i in range (n - x):
for j in range (x, n):
print (a[i][j], end = " " )
print ()
def printMatrix(a, n):
for i in range (n):
for j in range (n):
print (a[i][j], end = " " )
print ()
if __name__ = = '__main__' :
n = 4
a = [[ 0 for i in range (n)]
for i in range (n)]
for i in range (n):
for j in range (n):
a[i][j] = (i * 10 + j)
print ( "Original Matrix:" )
printMatrix(a, n)
x = 2
remove_X_Rows_and_Columns(a, n, x)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static void remove_X_Rows_and_Columns( int [,] a, int n, int x)
{
Console.Write( "\nRemoving Last " + x
+ " rows and First " + x
+ " columns:\n" );
for ( int i = 0; i < n - x; i++) {
for ( int j = x; j < n; j++) {
Console.Write(a[i,j]+ " " );
}
Console.WriteLine();
}
}
static void printMatrix( int [,] a, int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
Console.Write(a[i,j]+ " " );
}
Console.WriteLine();
}
}
public static void Main(String[] args)
{
int n = 4;
int [,]a= new int [n,n];
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
a[i,j] = (i * 10 + j);
}
}
Console.Write( "Original Matrix:\n" );
printMatrix(a, n);
int x = 2;
remove_X_Rows_and_Columns(a, n, x);
}
}
|
Javascript
<script>
function remove_X_Rows_and_Columns(a, n, x)
{
document.write( "</br>" + "Removing Last " + x
+ " rows and First " + x
+ " columns:" + "</br>" );
for (let i = 0; i < n - x; i++) {
for (let j = x; j < n; j++) {
document.write(a[i][j] + " " );
}
document.write( "</br>" );
}
}
function printMatrix(a, n)
{
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
document.write(a[i][j] + " " );
}
document.write( "</br>" );
}
}
let n = 4;
let a = new Array(n);
for (let i = 0; i < n; i++) {
a[i] = new Array(n);
for (let j = 0; j < n; j++) {
a[i][j] = (i * 10 + j);
}
}
document.write( "Original Matrix:" + "</br>" );
printMatrix(a, n);
let x = 2;
remove_X_Rows_and_Columns(a, n, x);
</script>
|
Output
Original Matrix:
0 1 2 3
10 11 12 13
20 21 22 23
30 31 32 33
Removing Last 2 rows and First 2 columns:
2 3
12 13
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!