# Find pair of rows in a binary matrix that has maximum bit difference in C++

C++Server Side ProgrammingProgramming

## Concept

With respect of a given Binary Matrix, our task is to determine the pair of row in the Binary matrix that has maximum bit difference.

## Input

matrix[][] = {{1, 1, 1, 1},
{1, 0, 0, 1},
{0, 0, 0, 0}};

## Output

(1, 3)

Bit difference between row numbers 1 and 3is maximum with value 4. Bit difference between 1 and 2 is 2 and between 2 and 3is 2.

## Input

matrix[][] = {{1 ,1 ,1 ,1 }
{1 ,0, 1 ,1 }
{0 ,1 ,0 ,0 }
{1, 0 ,0 ,0 }}

## Output

(2, 3)

Bit difference between rows 2 and 3 is maximum which is 4.

## Input

matrix[][] = {{1 ,0 ,1 ,1 }
{1 ,0 ,1 ,1 }
{0 ,0 ,0 ,1 }
{1, 0 ,0 ,0 }}

## Output

(2, 3) or (1,3) or (1,4) or (2,4)

They all are having bit difference is 2.

## Method

According to simple solution of this problem, we pick each row of binary matrix one -by -one and calculate maximum bit difference with rest of the rows of matrix.

Finally return rows those have maximum bit difference.

An efficient solution implements Trie Data Structure. Following is algorithm.

• Here, we create an empty Trie. It should be noted that every node of Trie consists two children for 0 and 1 bits.

• Next, we insert first Row of binary matrix into Trie

• Visit remaining of the rows of given Binary Matrix

• With respect of each row, at first we search maximum bit difference with rows that we insert before that in Trie and count bit difference

• Now, for every search we modify maximum bit_diff count if required else not store pair of index that have maximum bit difference

• Finally, print pair

## Example

Live Demo

// C++ program to Find Pair of row in Binary matrix
// that has maximum Bit difference
#include<bits/stdc++.h>
using namespace std;
// Indicates maximum size of matrix
const int MAX1 = 100;
struct TrieNode{
int leaf1; //store index of visited row
struct TrieNode *Child1;
};
// Shows utility function to create a new Trie node
TrieNode * getNode(){
TrieNode * newNode1 = new TrieNode;
newNode1->leaf1 = 0;
newNode1->Child1 = newNode1->Child1 = NULL;
return newNode1;
}
// Shows utility function insert new row in trie
void insert(TrieNode *root1, int Mat1[][MAX1], int n1,
int row_index1){
TrieNode * temp1 = root1;
for (int i=0; i<n1; i++){
// Append a new Node into trie
if(temp1->Child1[ Mat1[row_index1][i] ] == NULL)
temp1->Child1[ Mat1[row_index1][i] ] = getNode();
// Used to move current node to point next node in trie
temp1 = temp1->Child1[ Mat1[row_index1][i] ];
}
// Here store index of currently inserted row
temp1->leaf1 = row_index1 +1 ;
}
// Shows utility function compute maximum bit difference of
// current row with previous visited row of binary matrix
pair<int, int> maxBitDiffCount(TrieNode * root1,
int Mat1[][MAX1], int n1, int row_index1){
TrieNode * temp1 = root1;
int count1 = 0;
// Determine previous visited row of binary matrix
// that has starting bit same as current row
for (int i= 0 ; i < n1 ; i++){
// At first look for same bit in trie
if (temp1->Child1[ Mat1[row_index1][i] ] != NULL)
temp1 = temp1->Child1[ Mat1[row_index1][i] ];
// Otherwise looking for opposite bit
else if (temp1->Child1[1 - Mat1[row_index1][i]] != NULL){
temp1 = temp1->Child1[1- Mat1[row_index1][i]];
count1++;
}
}
int leaf_index1 = temp1->leaf1;
int count2 = 0 ;
temp1 = root1;
// Determine previous visited row of binary matrix
// that has starting bit opposite to current row
for (int i= 0 ; i < n1 ; i++){
// At first looking for opposite bit
if (temp1->Child1[ 1 - Mat1[row_index1][i] ] !=NULL){
temp1 = temp1->Child1[ 1- Mat1[row_index1][i] ];
count2++;
}
// Else look for same bit in trie
else if (temp1->Child1[ Mat1[row_index1][i] ] != NULL)
temp1 = temp1->Child1[ Mat1[row_index1][i] ];
}
pair <int ,int> P = count2 > count1 ?
make_pair(count2, temp1->leaf1):
make_pair(count1, leaf_index1);
// Here return pair that contain both bit difference
// count and index of row with we get bit
// difference
return P;
}
// Returns maximum bit difference pair of row
void maxDiff( int mat1[][MAX1], int n1, int m1){
TrieNode * root1 = getNode();
// Used to insert first matrix row in trie
insert(root1, mat1, m1, 0);
int max_bit_diff1= INT_MIN;
pair <int ,int> P, temp1 ;
// Visit all rest row of binary matrix
for (int i = 1 ; i < n1; i++){
// Calculate bit difference with previous visited
// rows of matrix
temp1 = maxBitDiffCount(root1, mat1, m1 ,i);
// Modify maximum bit difference
if (max_bit_diff1 < temp1.first ){
max_bit_diff1 = temp1.first;
P = make_pair( temp1.second, i+1);
}
// Used to insert current row value into Trie
insert(root1, mat1, m1, i );
}
// Now print maximum bit difference pair in row
cout << "(" << P.first <<", "<< P.second << ")";
}
// Driver program
int main(){
int mat1[][MAX1] = {{1 ,0 ,1 ,1 },{1, 0, 1 ,1 },{0 ,0 ,0 ,1},{1 ,0 ,0 ,0}};
maxDiff(mat1, 4, 4) ;
/* int mat1[][MAX1] = {{1 ,0 ,1 ,1 },{1, 1, 1 ,1 },{0 ,1 ,0 ,1},{1 ,0 ,0 ,0}};
maxDiff(mat1, 4, 4) ; */
/*int mat1[][MAX1] = {{1 ,1, 1 ,1 },{1, 0, 1 ,1 },{0 ,0 ,0 ,0},};
maxDiff(mat1, 3, 4) ; */
return 0;
}

## Output

(2, 3)