Unique cells in a binary matrix | Efficient C # 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
using System ; public class GFG { static readonly int MAX = 100 ; static bool isUnique ( int [ , ] mat , int i , int j , int n , int m ) { int sumrow = 0 ; for ( int k = 0 ; k < m ; k ++ ) { sumrow += mat [ i , k ] ; if ( sumrow > 1 ) return false ; } int sumcol = 0 ; for ( int k = 0 ; k < n ; k ++ ) { sumcol += mat [ k , j ] ; if ( sumcol > 1 ) return false ; } return true ; } static int countUnique ( int [ , ] mat , int n , int m ) { int uniquecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( mat [ i , j ] != 0 && isUnique ( mat , i , j , n , m ) ) uniquecount ++ ; return uniquecount ; } static public void Main ( ) { int [ , ] mat = { { 0 , 1 , 0 , 0 } , { 0 , 0 , 1 , 0 } , { 1 , 0 , 0 , 1 } } ; Console . Write ( countUnique ( mat , 3 , 4 ) ) ; } }