Count positions in Binary Matrix having equal count of set bits in corresponding row and column | Java Program to implement above approach ; Function to return the count of indices in from the given binary matrix having equal count of set bits in its row and column ; Stores count of set bits in corresponding column and row ; Traverse matrix ; Since 1 contains a set bit ; Update count of set bits for current row and col ; Stores the count of required indices ; Traverse matrix ; If current row and column has equal count of set bits ; Return count of required position ; Driver Code
import java . util . * ; import java . lang . * ; class GFG { static int countPosition ( int [ ] [ ] mat ) { int n = mat . length ; int m = mat [ 0 ] . length ; int [ ] row = new int [ n ] ; int [ ] col = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { col [ j ] ++ ; row [ i ] ++ ; } } } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( row [ i ] == col [ j ] ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 1 } , { 1 , 1 } } ; System . out . println ( countPosition ( mat ) ) ; } }