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

C++Server Side ProgrammingProgramming

Suppose we have a binary matrix; we have to find the pair of rows in the given matrix that has maximum bit difference.

So, if the input is like matrix, then the output will be [2,3] as bit difference between rows 2 and row 3 is 4, this is maximum.

  • To solve this, we will follow these steps −

  • Define Trie structure, with value and two children.

  • Define a function get_max_bit_diff(), this will take root of a trie, matrix, n, row_index,

  • temp := root, count := 0

  • for initialize i := 0, when i < n, update (increase i by 1), do−

    • if child[ matrix[row_index, i] ] of temp is not NULL, then −

      • temp := child[ matrix[row_index, i] ] of temp

    • otherwise when child[1 - matrix[row_index, i]] of temp is not NULL, then −

      • temp := child[1- matrix[row_index, i]] of temp

      • (increase count by 1)

  • leaf_index := leaf of temp

  • temp_count := 0, temp := root

  • for initialize i := 0, when i < n, update (increase i by 1), do−

    • if child[ 1 - matrix[row_index, i] ] of temp is not NULL, then −

      • temp := child[ 1- matrix[row_index, i] ] of temp

      • (increase temp_count by 1)

    • otherwise when child[ matrix[row_index, i] ] of temp is not NULL, then −

      • temp := child[ matrix[row_index, i] ] of temp

  • P = if temp_count > count then make a pair using (temp_count, leaf of temp) otherwise make a pair using (count, leaf_index)

  • return P

  • From the main method, do the following −

  • root = a new TrieNode

  • insert 0th row into root

  • max_bit_diff := -inf

  • Define one pair pr and another pair temp

  • for initialize i := 1, when i < n, update (increase i by 1), do−

    • temp := get_max_bit_diff(root, mat, m, i)

    • if max_bit_diff < first of temp, then −

      • max_bit_diff := temp.first

      • pr := make a pair using (temp.second, i + 1)

    • insert ith row into root

  • display pair pr

Example (C++)

Let us see the following implementation to get better understanding −

 Live Demo

#include<bits/stdc++.h>
using namespace std;
const int MAX = 100;
class TrieNode {
   public:
   int leaf;
   TrieNode *child[2];
   TrieNode(){
      leaf = 0;
      child[0] = child[1] = NULL;
   }
};
void insert(TrieNode *root, int matrix[][MAX], int n, int row_index){
   TrieNode * temp = root;
   for (int i=0; i<n; i++) {
      if(temp->child[ matrix[row_index][i] ] == NULL)
         temp->child[ matrix[row_index][i] ] = new TrieNode();
         temp = temp->child[ matrix[row_index][i] ];
      }
      temp->leaf = row_index +1 ;
   }
   pair<int, int>get_max_bit_diff(TrieNode * root, int matrix[][MAX], int n, int row_index) {
      TrieNode * temp = root;
      int count = 0;
      for (int i= 0 ; i < n ; i++) {
         if (temp->child[ matrix[row_index][i] ] != NULL)
            temp = temp->child[ matrix[row_index][i] ];
         else if (temp->child[1 - matrix[row_index][i]] != NULL) {
            temp = temp->child[1- matrix[row_index][i]];
            count++;
         }
      }
      int leaf_index = temp->leaf;
      int temp_count = 0 ;
      temp = root;
      for (int i= 0 ; i < n ; i++) {
         if (temp->child[ 1 - matrix[row_index][i] ] !=NULL) {
            temp = temp->child[ 1- matrix[row_index][i] ];
            temp_count++;
         }
         else if (temp->child[ matrix[row_index][i] ] != NULL)
            temp = temp->child[ matrix[row_index][i] ];
      }
      pair <int ,int> P = temp_count > count ? make_pair(temp_count, temp->leaf): make_pair(count, leaf_index);
      return P;
}
void get_max_diff( int mat[][MAX], int n, int m) {
   TrieNode * root = new TrieNode();
   insert(root, mat, m, 0);
   int max_bit_diff = INT_MIN;
   pair<int ,int> pr, temp ;
   for (int i = 1 ; i < n; i++) {
      temp = get_max_bit_diff(root, mat, m ,i);
      if (max_bit_diff < temp.first ) {
         max_bit_diff = temp.first;
         pr = make_pair( temp.second, i+1);
      }
      insert(root, mat, m, i );
   }
   cout << "(" << pr.first <<", "<< pr.second << ")";
}
int main() {
   int mat[][MAX] = {
      {1 ,1 ,1 ,1 },
      {1, 0, 1 ,1},
      {0 ,1 ,0 ,0},
      {1 ,0 ,0 ,0}
   };
   get_max_diff(mat, 4, 4) ;
}

Input

{{1 ,1 ,1 ,1 },
{1, 0, 1 ,1},
{0 ,1 ,0 ,0},
{1 ,0 ,0 ,0}}, 4,4

Output

(2,3)
raja
Updated on 25-Aug-2020 12:06:43

Advertisements