Remove any corner X rows and columns from a matrix
Given a NxN matrix and an integer X. The task is to remove X rows and X columns from the NxN matrix.
- Remove 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;
// Function to remove first x
// rows and columns from an array
void
remove_X_Rows_and_Columns(
int
* a,
int
n,
int
x)
{
cout <<
"\nRemoving First "
<< x
<<
" rows and columns:\n"
;
// Start from the xth row
// and print till n-1th row
for
(
int
i = x; i < n; i++) {
// Start from the xth column
// and print till the n-1th column
for
(
int
j = x; j < n; j++) {
// Accessing the array using pointers
cout << *((a + i * n) + j) <<
" "
;
}
cout << endl;
}
}
// Print the matrix
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;
// get the array inputs
int
a[n][n];
for
(
int
i = 0; i < n; i++) {
for
(
int
j = 0; j < n; j++) {
a[i][j] = (i * 10 + j);
}
}
// Print the matrix
cout <<
"Original Matrix:\n"
;
printMatrix((
int
*)a, n);
int
x = 2;
// passing the two dimensional
// array using a single pointer
remove_X_Rows_and_Columns((
int
*)a, n, x);
return
0;
}
chevron_rightfilter_nonePython3
# Python3 implementation of the above approach
# Function to remove first x
# rows and columns from an array
def
remove_X_Rows_and_Columns(a, n, x):
print
(
"Removing First "
, x,
" rows and columns:"
)
# Start from the xth row
# and pr till n-1th row
for
i
in
range
(x, n):
# Start from the xth column
# and pr till the n-1th column
for
j
in
range
(x, n):
# Accessing the array using poers
print
(a[i][j], end
=
" "
)
print
()
# Print the matrix
def
prMatrix(a, n):
for
i
in
range
(n):
for
j
in
range
(n):
print
(a[i][j], end
=
" "
)
print
()
# Driver Code
if
__name__
=
=
'__main__'
:
n
=
4
# get the array inputs
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 the matrix
print
(
"Original Matrix:"
)
prMatrix(a, n)
x
=
2
# passing the two dimensional
# array using a single poer
remove_X_Rows_and_Columns(a, n, x)
# This code is contributed by
# Mohit kumar 29
chevron_rightfilter_nonePHP
<?php
// PHP implementation of the above approach
// Function to remove first x
// rows and columns from an array
function
remove_X_Rows_and_Columns(
$a
,
$n
,
$x
)
{
echo
"\nRemoving First "
,
$x
,
" rows and columns:\n"
;
// Start from the xth row
// and print till n-1th row
for
(
$i
=
$x
;
$i
<
$n
;
$i
++)
{
// Start from the xth column
// and print till the n-1th column
for
(
$j
=
$x
;
$j
<
$n
;
$j
++)
{
// Accessing the array using pointers
echo
$a
[
$i
][
$j
],
" "
;
}
echo
"\n"
;
}
}
// Print the matrix
function
printMatrix(
$a
,
$n
)
{
for
(
$i
= 0;
$i
<
$n
;
$i
++)
{
for
(
$j
= 0;
$j
<
$n
;
$j
++)
{
echo
$a
[
$i
][
$j
],
" "
;
}
echo
"\n"
;
}
}
$n
= 4;
// get the array inputs
$a
=
array
(
array
());
for
(
$i
= 0;
$i
<
$n
;
$i
++)
{
for
(
$j
= 0;
$j
<
$n
;
$j
++)
{
$a
[
$i
][
$j
] =
$i
* 10 +
$j
;
}
}
// Print the matrix
echo
"Original Matrix:\n"
;
printMatrix(
$a
,
$n
);
$x
= 2;
// passing the two dimensional
// array using a single pointer
remove_X_Rows_and_Columns(
$a
,
$n
,
$x
);
// This code is contributed by Ryuga
?>
chevron_rightfilter_noneOutput: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 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:
#include <iostream>
using
namespace
std;
// Function to remove last x rows
// and columns from an array
void
remove_X_Rows_and_Columns(
int
* a,
int
n,
int
x)
{
cout <<
"\nRemoving Last "
<< x
<<
" rows and columns:\n"
;
// Start from the 0th row
// and print till n-xth row
for
(
int
i = 0; i < n - x; i++) {
// Start from the 0th column
// and print till the n-xth column
for
(
int
j = 0; j < n - x; j++) {
// Accessing the array using pointers
cout << *((a + i * n) + j) <<
" "
;
}
cout << endl;
}
}
// Print the matrix
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;
// get the array inputs
int
a[n][n];
for
(
int
i = 0; i < n; i++) {
for
(
int
j = 0; j < n; j++) {
a[i][j] = (i * 10 + j);
}
}
// Print the matrix
cout <<
"Original Matrix:\n"
;
printMatrix((
int
*)a, n);
int
x = 2;
// passing the two dimensional
// array using a single pointer
remove_X_Rows_and_Columns((
int
*)a, n, x);
return
0;
}
chevron_rightfilter_noneOutput: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 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:
#include <iostream>
using
namespace
std;
// Function to remove first x rows
// and last x columns from an array
void
remove_X_Rows_and_Columns(
int
* a,
int
n,
int
x)
{
cout <<
"\nRemoving First "
<< x
<<
" rows and Last "
<< x
<<
" columns:\n"
;
// Start from the xth row
// and print till n-1th row
for
(
int
i = x; i < n; i++) {
// Start from the 0th column
// and print till the n-xth column
for
(
int
j = 0; j < n - x; j++) {
// Accessing the array using pointers
cout << *((a + i * n) + j) <<
" "
;
}
cout << endl;
}
}
// Print the matrix
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;
// get the array inputs
int
a[n][n];
for
(
int
i = 0; i < n; i++) {
for
(
int
j = 0; j < n; j++) {
a[i][j] = (i * 10 + j);
}
}
// Print the matrix
cout <<
"Original Matrix:\n"
;
printMatrix((
int
*)a, n);
int
x = 2;
// passing the two dimensional
// array using a single pointer
remove_X_Rows_and_Columns((
int
*)a, n, x);
return
0;
}
chevron_rightfilter_noneOutput: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 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:
#include <iostream>
using
namespace
std;
// Function to remove last x rows
// and first x columns from an array
void
remove_X_Rows_and_Columns(
int
* a,
int
n,
int
x)
{
cout <<
"\nRemoving Last "
<< x
<<
" rows and First "
<< x
<<
" columns:\n"
;
// Start from the 0th row
// and print till n-xth row
for
(
int
i = 0; i < n - x; i++) {
// Start from the xth column and
// print till the n-1th column
for
(
int
j = x; j < n; j++) {
// Accessing the array using pointers
cout << *((a + i * n) + j) <<
" "
;
}
cout << endl;
}
}
// Print the matrix
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;
// get the array inputs
int
a[n][n];
for
(
int
i = 0; i < n; i++) {
for
(
int
j = 0; j < n; j++) {
a[i][j] = (i * 10 + j);
}
}
// Print the matrix
cout <<
"Original Matrix:\n"
;
printMatrix((
int
*)a, n);
int
x = 2;
// passing the two dimensional
// array using a single pointer
remove_X_Rows_and_Columns((
int
*)a, n, x);
return
0;
}
chevron_rightfilter_noneOutput: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
Recommended Posts:
- Remove first X rows and columns from a matrix
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Sorting rows of matrix in ascending order followed by columns in descending order
- Sorting rows of matrix in descending order followed by columns in ascending order
- Count rows/columns with sum equals to diagonal sum
- Print the corner elements and their sum in a 2-D matrix
- Sum of columns of a 2-D Matrix where first element is odd
- Check if matrix A can be converted to B by changing parity of corner elements of any submatrix
- Interchange elements of first and last columns in matrix
- Count all the columns in a matrix which are sorted in descending
- Common elements in all rows of a given matrix
- Find all permuted rows of a given row in a matrix
- Count all sorted rows in a matrix
- Interchange elements of first and last rows in matrix
- Print index of columns sorted by count of zeroes in the Given Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.