Remove all zero | Java 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
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 ) ; } }