Remove all zero | C ++ program for the above approach ; Function to remove the rows or columns from the matrix which contains all 0 s elements ; Stores count of rows ; col [ i ] : Stores count of 0 s in current column ; row [ i ] : Stores count of 0 s in current row ; Traverse the matrix ; Stores count of 0 s in current row ; Update col [ j ] ; Update count ; Update row [ i ] ; Traverse the matrix ; If all elements of current row is 0 ; If all elements of current column is 0 ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE 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 << " STRNEWLINE " ; } } 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 ; }