Maximize count of rows consisting of equal elements by flipping columns of a Matrix
Given a binary matrix, mat[][] of dimensions N * M, the task is to maximize the count of rows consisting only of equal elements by selecting any column of the matrix and flipping all the elements of that column in each operation. Print the maximum number of rows that can be made to form equal elements.
Examples:
Input: mat[][] = { { 0, 1, 0, 0, 1 }, { 1, 1, 0, 1, 1 }, { 1, 0, 1, 1, 0 } }
Output: 2
Explanation:
Select the 2nd column and flip all the elements of that column to modify mat[][] to { { 0, 0, 0, 0, 1 }, { 1, 0, 0, 1, 1 }, { 1, 1, 1, 1, 0 } }
Select the 5th column and flip all the elements of that column to modify mat[][] to { { 0, 0, 0, 0, 0 }, { 1, 0, 0, 1, 0 }, { 1, 1, 1, 1, 1 } }
Since all elements of the 1st row and the 3rd row of the matrix are equal and is also the maximum number of rows that can be made to contain equal elements only, the required output is 2.
Input: mat[][] = { {0, 0}, {0, 1} }
Output: 1
Naive Approach: The simplest approach to solve this problem is to count the number of rows which contains equal elements only, for every possible way of selecting a combination of columns and flipping its elements. Finally, print the maximum count obtained for any of the above combinations.
Time Complexity: O(N * M * 2M)
Auxiliary Space: O(N * M)
Efficient Approach: To optimize the above approach, the idea is based on the fact that, if one row is 1‘s complement of the other row or both rows are the same, then only both the rows will contain equal elements only by performing the given operations.
Illustration:
Let us consider the following matrix:
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
In the above matrix, 1st and 2nd rows are 1’s complement of each other, and the 5th and 4th rows are 1’s complement of each other.
Follow the steps below to solve the problem:
- Initialize a variable, say cntMaxRows, to store the maximum count of rows consisting of equal elements only.
- Initialize a Map, say mp, to store all possible rows of the matrix.
- Traverse each row of the matrix and store it in the Map.
- Traverse each row of the matrix using variable row. Calculate 1‘s complement of row and update cntMaxRows = max(cntMaxRows, mp[row] + mp[1’s_comp_row])
- Finally, print the value of cntMaxRows.
Below is the implementation of our approach:
C++
#include <bits/stdc++.h>
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);
}
|
Java
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));
}
}
|
Python3
from collections import defaultdict
def maxEqrows(mat, N, M):
mp = defaultdict( lambda : 0 )
for i in range (N):
key = tuple (mat[i])
mp[key] + = 1 ;
cntMaxRows = 0 ;
for i in range (N):
onesCompRow = []
for j in range (M):
onesCompRow.append(mat[i][j] ^ 1 );
key = tuple (mat[i])
cntMaxRows = max (cntMaxRows, mp[key] + mp[ tuple (onesCompRow)]);
return cntMaxRows;
mat = []
mat.append([ 0 , 1 , 0 , 0 , 1 ]);
mat.append([ 1 , 1 , 0 , 1 , 1 ]);
mat.append([ 1 , 0 , 1 , 1 , 0 ]);
N = len (mat);
M = len (mat[ 0 ]);
print (maxEqrows(mat, N, M));
|
C#
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));
}
}
|
Javascript
function maxEqrows(mat, N, M)
{
let mp = {}
for (let i = 0; i < N; i++)
{
let key = mat[i].join( "#" )
if (mp.hasOwnProperty(key)) {
mp[key] += 1;
}
else {
mp[key] = 1;
}
}
let cntMaxRows = 0;
for (let i = 0; i < N; i++) {
let onesCompRow = []
for (let j = 0; j < M; j++) {
onesCompRow.push(mat[i][j] ^ 1);
}
let key = mat[i].join( "#" )
if (!mp.hasOwnProperty(key)) {
cntMaxRows
= Math.max(cntMaxRows, mp[onesCompRow] + 1);
}
else if (!mp.hasOwnProperty(onesCompRow)) {
cntMaxRows
= Math.max(cntMaxRows, mp[key] + 1);
}
else {
cntMaxRows = Math.max(
cntMaxRows,
mp[(mat[i])] + mp[onesCompRow] + 1);
}
}
return cntMaxRows;
}
let mat = []
mat.push([ 0, 1, 0, 0, 1 ]);
mat.push([ 1, 1, 0, 1, 1 ]);
mat.push([ 1, 0, 1, 1, 0 ]);
let N = mat.length;
let M = mat[0].length;
console.log(maxEqrows(mat, N, M));
|
Time Complexity: O(N * M)
Auxiliary Space: O(M)
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...