
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find pair of rows in a binary matrix that has maximum bit difference in C++
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 −
#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)
- Related Articles
- Find pair with maximum difference in any column of a Matrix in C++
- Find duplicate rows in a binary matrix in C++
- Maximum difference of sum of elements in two rows in a matrix in C
- Find Maximum difference pair in Python
- Find maximum path length in a binary matrix in Python
- Write Python program to find duplicate rows in a binary matrix
- Find the Pair with a Maximum Sum in a Matrix using C++
- How to find the row that has maximum number of duplicates in an R matrix?
- Find row number of a binary matrix having maximum number of 1s in C++
- Find alphabet in a Matrix which has maximum number of stars around it in C++
- Maximum decimal value path in a binary matrix in C++
- Find a specific pair in Matrix in C++
- Count ways of choosing a pair with maximum difference in C++
- Find Number of Sorted Rows in a Matrix in Java?
- Remove One Bit from a Binary Number to Get Maximum Value in C++
