Remove all zero | 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
def removeZeroRowCol ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE col = [ 0 ] * ( n + 1 ) NEW_LINE row = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT col [ j ] += ( arr [ i ] [ j ] == 1 ) NEW_LINE count += ( arr [ i ] [ j ] == 1 ) NEW_LINE DEDENT row [ i ] = count NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( row [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( n ) : NEW_LINE INDENT if ( col [ j ] != 0 ) : NEW_LINE INDENT print ( arr [ i ] [ j ] , end = " " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT arr = [ [ 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 1 ] ] NEW_LINE removeZeroRowCol ( arr ) NEW_LINE