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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxEqrows ( vector < vector < int > > & mat , int N , int M ) { map < vector < int > , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ mat [ i ] ] ++ ; } int cntMaxRows = 0 ; for ( int i = 0 ; i < N ; i ++ ) { vector < int > onesCompRow ( M , 0 ) ; for ( int j = 0 ; j < M ; j ++ ) { onesCompRow [ j ] = ( mat [ i ] [ j ] ^ 1 ) ; } cntMaxRows = max ( cntMaxRows , mp [ mat [ i ] ] + mp [ onesCompRow ] ) ; } return cntMaxRows ; } int main ( ) { vector < vector < int > > mat = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 1 , 0 , 1 , 1 } , { 1 , 0 , 1 , 1 , 0 } } ; int N = mat . size ( ) ; int M = mat [ 0 ] . size ( ) ; cout << maxEqrows ( mat , N , M ) ; }