Unique cells in a binary matrix | Efficient Javascript program to count unique cells in a binary matrix ; Returns true if mat [ i ] [ j ] is unique ; Checking in row calculating sumrow will be moving column wise ; Checking in column calculating sumcol will be moving row wise ; Driver code
let MAX = 100 ; function isUnique ( mat , i , j , n , m ) { let sumrow = 0 ; for ( let k = 0 ; k < m ; k ++ ) { sumrow += mat [ i ] [ k ] ; if ( sumrow > 1 ) return false ; } let sumcol = 0 ; for ( let k = 0 ; k < n ; k ++ ) { sumcol += mat [ k ] [ j ] ; if ( sumcol > 1 ) return false ; } return true ; } function countUnique ( mat , n , m ) { let uniquecount = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < m ; j ++ ) if ( mat [ i ] [ j ] != 0 && isUnique ( mat , i , j , n , m ) ) uniquecount ++ ; return uniquecount ; } let mat = [ [ 0 , 1 , 0 , 0 ] , [ 0 , 0 , 1 , 0 ] , [ 1 , 0 , 0 , 1 ] ] ; document . write ( countUnique ( mat , 3 , 4 ) ) ;