Given a matrix arr[][] of size N * M, the task is to print the matrix after removing all rows and columns from the matrix which consists of 0s only.
Examples:
Input: arr[][] ={ { 1, 1, 0, 1 }, { 0, 0, 0, 0 }, { 1, 1, 0, 1}, { 0, 1, 0, 1 } }
Output:
111
111
011
Explanation:
Initially, the matrix is as follows:
arr[][] = { { 1, 1, 0, 1 },
{ 0, 0, 0, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 1 } }
Removing the 2nd row modifies the matrix to:
arr[][] = { { 1, 1, 0, 1 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 1 } }
Removing the 3rd column modifies the matrix to:
arr[][] = { { 1, 1, 1 },
{ 1, 1, 1 },
{ 0, 1, 1 } }
Input: arr={{0, 1}, {0, 1}}
Output:
1
1
Approach: The idea is to count the number of 0s in all the rows and columns of the matrix and check if any rows or columns consist only of 0s or not. If found to be true, then remove those rows or the columns of the matrix. Follow the steps below to solve the problem:
- Traverse the matrix and count 1s in rows and columns.
- Now, traverse over the loop again and check for the following:
- If the count of 1s is found to be 0 for any row, skip that row.
- If the count of 1s is found to be greater than 0 for any column, print that element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void removeZeroRowCol(vector<vector< int > >& arr)
{
int n = arr.size();
int col[n + 1] = { 0 };
int row[n + 1] = { 0 };
for ( int i = 0; i < n; ++i) {
int count = 0;
for ( int j = 0; j < n; ++j) {
col[j] += (arr[i][j] == 1);
count += (arr[i][j] == 1);
}
row[i] = count;
}
for ( int i = 0; i < n; ++i) {
if (row[i] == 0) {
continue ;
}
for ( int j = 0; j < n; ++j) {
if (col[j] != 0)
cout << arr[i][j];
}
cout << "\n" ;
}
}
int main()
{
vector<vector< int > > arr = { { 1, 1, 0, 1 },
{ 0, 0, 0, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 1 } };
removeZeroRowCol(arr);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void removeZeroRowCol( int arr[][])
{
int n = arr.length;
int col[] = new int [n + 1 ];
int row[] = new int [n + 1 ];
for ( int i = 0 ; i < n; ++i)
{
int count = 0 ;
for ( int j = 0 ; j < n; ++j)
{
if (arr[i][j] == 1 )
col[j] += 1 ;
else
col[j] += 0 ;
if (arr[i][j] == 1 )
count += 1 ;
else
count += 0 ;
}
row[i] = count;
}
for ( int i = 0 ; i < n; ++i)
{
if (row[i] == 0 )
{
continue ;
}
for ( int j = 0 ; j < n; ++j)
{
if (col[j] != 0 )
System.out.print(arr[i][j]);
}
System.out.println();
}
}
public static void main (String[] args)
{
int arr[][] = { { 1 , 1 , 0 , 1 },
{ 0 , 0 , 0 , 0 },
{ 1 , 1 , 0 , 1 },
{ 0 , 1 , 0 , 1 } };
removeZeroRowCol(arr);
}
}
|
Python3
def removeZeroRowCol(arr) :
n = len (arr)
col = [ 0 ] * (n + 1 )
row = [ 0 ] * (n + 1 )
for i in range (n) :
count = 0
for j in range (n) :
col[j] + = (arr[i][j] = = 1 )
count + = (arr[i][j] = = 1 )
row[i] = count
for i in range (n) :
if (row[i] = = 0 ) :
continue
for j in range (n) :
if (col[j] ! = 0 ) :
print (arr[i][j], end = "")
print ()
arr = [ [ 1 , 1 , 0 , 1 ],
[ 0 , 0 , 0 , 0 ],
[ 1 , 1 , 0 , 1 ],
[ 0 , 1 , 0 , 1 ] ]
removeZeroRowCol(arr)
|
C#
using System;
class GFG{
static void removeZeroRowCol( int [,] arr)
{
int n = arr.GetLength(0);
int [] col = new int [n + 1];
int [] row = new int [n + 1];
for ( int i = 0; i < n ; ++i)
{
int count = 0;
for ( int j = 0; j < n ; ++j)
{
if (arr[i, j] == 1)
col[j] += 1;
else
col[j] += 0;
if (arr[i, j] == 1)
count += 1;
else
count += 0;
}
row[i] = count;
}
for ( int i = 0; i < n; ++i)
{
if (row[i] == 0)
{
continue ;
}
for ( int j = 0; j < n; ++j)
{
if (col[j] != 0)
Console.Write(arr[i, j]);
}
Console.WriteLine();
}
}
public static void Main (String[] args)
{
int [,] arr = { { 1, 1, 0, 1 },
{ 0, 0, 0, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 1 } };
removeZeroRowCol(arr);
}
}
|
Javascript
<script>
function removeZeroRowCol(arr)
{
let n = arr.length;
let col = Array.from({length: n+1}, (_, i) => 0);
let row = Array.from({length: n+1}, (_, i) => 0);
for (let i = 0; i < n; ++i)
{
let count = 0;
for (let j = 0; j < n; ++j)
{
if (arr[i][j] == 1)
col[j] += 1;
else
col[j] += 0;
if (arr[i][j] == 1)
count += 1;
else
count += 0;
}
row[i] = count;
}
for (let i = 0; i < n; ++i)
{
if (row[i] == 0)
{
continue ;
}
for (let j = 0; j < n; ++j)
{
if (col[j] != 0)
document.write(arr[i][j]);
}
document.write( "<br/>" );
}
}
let arr = [[ 1, 1, 0, 1 ],
[ 0, 0, 0, 0 ],
[ 1, 1, 0, 1 ],
[ 0, 1, 0, 1 ]];
removeZeroRowCol(arr);
</script>
|
Time complexity: O(N*M)
Auxiliary Space: O(N+M)
Another efficient approach: The idea is to mark all rows and columns that contain all zeros with some other special integer (a value that is not present in the matrix) to identify that we do not need that particular row or column and whenever we reach While iterating over the matrix on that particular integer we skip that cell because we assume that the cell has been removed while deletion of the row or column which has all zeros.
Follow the below steps to implement the idea:
- Iterate over all rows one by one and check if that particular row contains all zeros or not.
If, particular row contains all zeros then fill special integer to mark that row containing all zeros.
- Iterate over all columns one by one and check if that particular column contains all zeros or not.
If, particular column contains all zeros then fill special integer to mark that column containing all zeros.
- Iterate over the matrix and check if any particular cell is marked with special integer.
If yes, then skip that cell.
Otherwise, print that cell.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void removeZeroRowCol(vector<vector< int > >& A)
{
int i, j, m, n;
m = A.size();
if (m != 0)
n = A[0].size();
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (A[i][j] == 1)
break ;
}
if (j == n) {
for (j = 0; j < n; j++)
A[i][j] = -1;
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (A[j][i] == 1)
break ;
}
if (j == n) {
for (j = 0; j < n; j++)
A[j][i] = -1;
}
}
}
void print(vector<vector< int > >& A)
{
int i, j, m, n;
m = A.size();
if (m != 0)
n = A[0].size();
bool flag = false ;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (A[i][j] != -1) {
cout << A[i][j];
flag = true ;
}
}
if (flag) {
cout << endl;
flag = !flag;
}
}
}
int main()
{
vector<vector< int > > arr{ { 1, 1, 0, 1 },
{ 0, 0, 0, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 1 } };
removeZeroRowCol(arr);
print(arr);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void removeZeroRowCol( int [][] A)
{
int i = 0 , j = 0 , m = A.length, n = 0 ;
if (m != 0 )
n = A[ 0 ].length;
for (i = 0 ; i < m; i++) {
for (j = 0 ; j < n; j++) {
if (A[i][j] == 1 )
break ;
}
if (j == n) {
for (j = 0 ; j < n; j++)
A[i][j] = - 1 ;
}
}
for (i = 0 ; i < m; i++) {
for (j = 0 ; j < n; j++) {
if (A[j][i] == 1 )
break ;
}
if (j == n) {
for (j = 0 ; j < n; j++)
A[j][i] = - 1 ;
}
}
}
private static void e() {
}
static void print( int [][] A)
{
int i = 0 , j = 0 , m = 0 , n = 0 ;
m = A.length;
if (m != 0 )
n = A[ 0 ].length;
boolean flag = false ;
for (i = 0 ; i < m; i++) {
for (j = 0 ; j < n; j++) {
if (A[i][j] != - 1 ) {
System.out.print(A[i][j]);
flag = true ;
}
}
if (flag) {
System.out.println();
flag = !flag;
}
}
}
public static void main(String args[])
{
int [][] arr = { { 1 , 1 , 0 , 1 },
{ 0 , 0 , 0 , 0 },
{ 1 , 1 , 0 , 1 },
{ 0 , 1 , 0 , 1 } };
removeZeroRowCol(arr);
print(arr);
}
}
|
Python3
def removeZeroRowCol(A):
m = len (A)
if (m ! = 0 ):
n = len (A[ 0 ])
i,j = 0 , 0
for i in range (m):
for j in range (n):
if (A[i][j] = = 1 ):
break
if (j = = n - 1 ):
for j in range (n):
A[i][j] = - 1
for i in range (m):
for j in range (n):
if (A[j][i] = = 1 ):
break
if (j = = n - 1 ):
for j in range (n):
A[j][i] = - 1
def Print (A):
m = len (A)
if (m ! = 0 ):
n = len (A[ 0 ])
flag = False
for i in range (m):
for j in range (n):
if (A[i][j] ! = - 1 ):
print (A[i][j],end = "")
flag = True
if (flag):
print ()
flag = False if (flag = = True ) else True
arr = [ [ 1 , 1 , 0 , 1 ],
[ 0 , 0 , 0 , 0 ],
[ 1 , 1 , 0 , 1 ],
[ 0 , 1 , 0 , 1 ] ]
removeZeroRowCol(arr)
Print (arr)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void removeZeroRowCol( int [,] A)
{
int i = 0, j = 0, m = A.GetLength(0), n = 0;
if (m != 0)
n = A.GetLength(1);
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (A[i,j] == 1)
break ;
}
if (j == n) {
for (j = 0; j < n; j++)
A[i,j] = -1;
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (A[j,i] == 1)
break ;
}
if (j == n) {
for (j = 0; j < n; j++)
A[j,i] = -1;
}
}
}
private static void e() {
}
static void print( int [,] A)
{
int i = 0, j = 0, m = 0, n = 0;
m = A.GetLength(0);
if (m != 0)
n = A.GetLength(1);
bool flag = false ;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (A[i,j] != -1) {
Console.Write(A[i,j]);
flag = true ;
}
}
if (flag) {
Console.WriteLine();
flag = !flag;
}
}
}
public static void Main( string [] args)
{
int [,] arr = { { 1, 1, 0, 1 },
{ 0, 0, 0, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 1 } };
removeZeroRowCol(arr);
print(arr);
}
}
|
Javascript
function removeZeroRowCol(A)
{
let m = A.length
if (m != 0)
n = A[0].length
let i = 0, j = 0
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (A[i][j] == 1)
break
}
if (j == n-1)
for (j = 0; j < n; j++)
A[i][j] = -1
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (A[j][i] == 1)
break
}
if (j == n-1)
for (j = 0; j < n; j++)
A[j][i] = -1
}
}
function Print(A)
{
let m = A.length
if (m != 0)
n = A[0].length
flag = false
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (A[i][j] != -1)
{
process.stdout.write(A[i][j] + "" )
flag = true
}
}
if (flag)
{
console.log()
flag = !flag
}
}
}
let arr = [ [ 1, 1, 0, 1 ],
[ 0, 0, 0, 0 ],
[ 1, 1, 0, 1 ],
[ 0, 1, 0, 1 ] ]
removeZeroRowCol(arr)
Print(arr)
|
Time complexity: O(N*M)
Auxiliary Space: O(1)
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 geeks!
Last Updated :
21 Dec, 2022
Like Article
Save Article