# Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first

Given two arrays **arr1[]** and **arr2[]**, the task is to take an element from first array **(say a)** and one element from second array **(say b)**. If the number formed by inverting the bits of **a** is equal to **b**, then the pair **(a, b)** is a valid pair.

**Inversion Of bits example:**

**11** is written as **1011** in binary. After inverting it’s bits, **0100** is obtained which is **4** in decimal. Hence **(11, 4)** is a valid pair but **(4, 11) is not** as **11** cannot be obtained after inverting the digits of **4** i.e. **100 -> 011** which is **3**.

**Examples:**

Input:arr1[] = {11, 5, 4}, arr2[] = {1, 4, 3, 11}

Output:2

(11, 4) and (4, 3) are the only valid pairs.

Input:arr1[] = {43, 7, 1, 99}, arr2 = {5, 1, 28, 20}

Output:2

**Approach:**

- Take two empty sets
**s1**and**s2**. - Insert all the elements of the
**arr2[]**in**s2**. - Iterate the first array. If the element is not present in the first set and the number formed by inverting it’s bits is present in the second set then increment the count and insert the current element in
**s1**so that it doesn’t get counted again. - Print the value of
**count**in the end.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the number formed ` `// by inverting bits the bits of num ` `int` `invertBits(` `int` `num) ` `{ ` ` ` `// Number of bits in num ` ` ` `int` `x = log2(num) + 1; ` ` ` ` ` `// Inverting the bits one by one ` ` ` `for` `(` `int` `i = 0; i < x; i++) ` ` ` `num = (num ^ (1 << i)); ` ` ` ` ` `return` `num; ` `} ` ` ` `// Function to return the total valid pairs ` `int` `totalPairs(` `int` `arr1[], ` `int` `arr2[], ` `int` `n, ` `int` `m) ` `{ ` ` ` ` ` `// Set to store the elements of the arrays ` ` ` `unordered_set<` `int` `> s1, s2; ` ` ` ` ` `// Insert all the elements of arr2[] in the set ` ` ` `for` `(` `int` `i = 0; i < m; i++) ` ` ` `s2.insert(arr2[i]); ` ` ` ` ` `// Initialize count variable to 0 ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Check if element of the first array ` ` ` `// is not in the first set ` ` ` `if` `(s1.find(arr1[i]) == s1.end()) { ` ` ` ` ` `// Check if the element formed by inverting bits ` ` ` `// is in the second set ` ` ` `if` `(s2.find(invertBits(arr1[i])) != s2.end()) { ` ` ` ` ` `// Increment the count of valid pairs and insert ` ` ` `// the element in the first set so that ` ` ` `// it doesn't get counted again ` ` ` `count++; ` ` ` `s1.insert(arr1[i]); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the total number of pairs ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr1[] = { 43, 7, 1, 99 }; ` ` ` `int` `arr2[] = { 5, 1, 28, 20 }; ` ` ` `int` `n = ` `sizeof` `(arr1) / ` `sizeof` `(arr1[0]); ` ` ` `int` `m = ` `sizeof` `(arr2) / ` `sizeof` `(arr2[0]); ` ` ` ` ` `cout << totalPairs(arr1, arr2, n, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `math ` `import` `log2; ` ` ` `# Function to return the number formed ` `# by inverting bits the bits of num ` `def` `invertBits(num) : ` ` ` ` ` `# Number of bits in num ` ` ` `x ` `=` `log2(num) ` `+` `1` `; ` ` ` ` ` `# Inverting the bits one by one ` ` ` `for` `i ` `in` `range` `(` `int` `(x)) : ` ` ` `num ` `=` `(num ^ (` `1` `<< i)); ` ` ` ` ` `return` `num; ` ` ` `# Function to return the total valid pairs ` `def` `totalPairs(arr1, arr2, n, m) : ` ` ` ` ` `# Set to store the elements of the arrays ` ` ` `s1, s2 ` `=` `set` `(), ` `set` `(); ` ` ` ` ` `# Insert all the elements of ` ` ` `# arr2[] in the set ` ` ` `for` `i ` `in` `range` `(m) : ` ` ` `s2.add(arr2[i]); ` ` ` ` ` `# Initialize count variable to 0 ` ` ` `count ` `=` `0` `; ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` ` ` `# Check if element of the first array ` ` ` `# is not in the first set ` ` ` `if` `arr1[i] ` `not` `in` `s1 : ` ` ` ` ` `# Check if the element formed by ` ` ` `# inverting bits is in the second set ` ` ` `if` `invertBits(arr1[i]) ` `in` `s2 : ` ` ` ` ` `# Increment the count of valid pairs ` ` ` `# and insert the element in the first ` ` ` `# set so that it doesn't get counted again ` ` ` `count ` `+` `=` `1` `; ` ` ` `s1.add(arr1[i]); ` ` ` ` ` `# Return the total number of pairs ` ` ` `return` `count; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr1 ` `=` `[ ` `43` `, ` `7` `, ` `1` `, ` `99` `]; ` ` ` `arr2 ` `=` `[ ` `5` `, ` `1` `, ` `28` `, ` `20` `]; ` ` ` `n ` `=` `len` `(arr1); ` ` ` `m ` `=` `len` `(arr2); ` ` ` ` ` `print` `(totalPairs(arr1, arr2, n, m)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Total distinct pairs of ugly numbers from two arrays
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Number of distinct integers obtained by lcm(X, N)/X
- Count distinct pairs from two arrays having same sum of digits
- Count of pairs between two arrays such that the sums are distinct
- Count pairs formed by distinct element sub-arrays
- Minimum number N such that total set bits of all numbers from 1 to N is at-least X
- Find the total marks obtained according to given marking scheme
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count pairs of elements such that number of set bits in their OR is B[i]
- Count pairs of elements such that number of set bits in their AND is B[i]
- Count number of distinct pairs whose sum exists in the given array
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.