Maximize count of rows consisting of equal elements by flipping columns of a Matrix | C # program to implement the above approach ; Function to find the maximum number of rows containing all equal elements ; Store each row of the matrix ; Traverse each row of the matrix ; Update frequency of current row ; Stores maximum count of rows containing all equal elements ; Traverse each row of the matrix ; Stores 1 's complement  of the current row ; Traverse current row of the given matrix ; Stores 1 s complement of mat [ i ] [ j ] ; Update cntMaxRows ; Driver code ; Stores count of rows ; Stores count of columns
using System ; using System . Collections . Generic ; class GFG { static int maxEqrows ( List < List < int > > mat , int N , int M ) { Dictionary < List < int > , int > mp = new Dictionary < List < int > , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( mat [ i ] ) ) { mp [ mat [ i ] ] ++ ; } else { mp [ mat [ i ] ] = 1 ; } } int cntMaxRows = 0 ; for ( int i = 0 ; i < N ; i ++ ) { List < int > onesCompRow = new List < int > ( ) ; for ( int j = 0 ; j < M ; j ++ ) { onesCompRow . Add ( 0 ) ; } for ( int j = 0 ; j < M ; j ++ ) { onesCompRow [ j ] = ( mat [ i ] [ j ] ^ 1 ) ; } if ( ! mp . ContainsKey ( mat [ i ] ) ) { cntMaxRows = Math . Max ( cntMaxRows , mp [ onesCompRow ] + 1 ) ; } else if ( ! mp . ContainsKey ( onesCompRow ) ) { cntMaxRows = Math . Max ( cntMaxRows , mp [ mat [ i ] ] + 1 ) ; } else { cntMaxRows = Math . Max ( cntMaxRows , mp [ mat [ i ] ] + mp [ onesCompRow ] + 1 ) ; } } return cntMaxRows ; } static void Main ( ) { List < List < int > > mat = new List < List < int > > ( ) ; mat . Add ( new List < int > { 0 , 1 , 0 , 0 , 1 } ) ; mat . Add ( new List < int > { 1 , 1 , 0 , 1 , 1 } ) ; mat . Add ( new List < int > { 1 , 0 , 1 , 1 , 0 } ) ; int N = mat . Count ; int M = mat [ 0 ] . Count ; Console . WriteLine ( maxEqrows ( mat , N , M ) ) ; } }