Maximize count of rows consisting of equal elements by flipping columns of a Matrix | Java program to implement ; 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
import java . util . * ; class GFG { static int maxEqrows ( Vector < Vector < Integer > > mat , int N , int M ) { HashMap < Vector < Integer > , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . containsKey ( mat . get ( i ) ) ) { mp . put ( mat . get ( i ) , mp . get ( mat . get ( i ) ) + 1 ) ; } else { mp . put ( mat . get ( i ) , 1 ) ; } } int cntMaxRows = 0 ; for ( int i = 0 ; i < N ; i ++ ) { Vector < Integer > onesCompRow = new Vector < Integer > ( ) ; for ( int j = 0 ; j < M ; j ++ ) { onesCompRow . add ( 0 ) ; } for ( int j = 0 ; j < M ; j ++ ) { onesCompRow . set ( j , mat . get ( i ) . get ( j ) ^ 1 ) ; } if ( ! mp . containsKey ( mat . get ( i ) ) ) { cntMaxRows = Math . max ( cntMaxRows , mp . get ( onesCompRow ) ) ; } else if ( ! mp . containsKey ( onesCompRow ) ) { cntMaxRows = Math . max ( cntMaxRows , mp . get ( mat . get ( i ) ) ) ; } else { cntMaxRows = Math . max ( cntMaxRows , mp . get ( mat . get ( i ) ) + mp . get ( onesCompRow ) ) ; } } return cntMaxRows ; } public static void main ( String [ ] args ) { Vector < Vector < Integer > > mat = new Vector < Vector < Integer > > ( ) ; mat . add ( new Vector < Integer > ( ) ) ; mat . add ( new Vector < Integer > ( ) ) ; mat . add ( new Vector < Integer > ( ) ) ; mat . get ( 0 ) . add ( 0 ) ; mat . get ( 0 ) . add ( 1 ) ; mat . get ( 0 ) . add ( 0 ) ; mat . get ( 0 ) . add ( 0 ) ; mat . get ( 0 ) . add ( 1 ) ; mat . get ( 1 ) . add ( 1 ) ; mat . get ( 1 ) . add ( 1 ) ; mat . get ( 1 ) . add ( 0 ) ; mat . get ( 1 ) . add ( 1 ) ; mat . get ( 1 ) . add ( 1 ) ; mat . get ( 2 ) . add ( 1 ) ; mat . get ( 2 ) . add ( 0 ) ; mat . get ( 2 ) . add ( 1 ) ; mat . get ( 2 ) . add ( 1 ) ; mat . get ( 2 ) . add ( 0 ) ; int N = mat . size ( ) ; int M = mat . get ( 0 ) . size ( ) ; System . out . println ( maxEqrows ( mat , N , M ) ) ; } }