Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
Last Updated :
10 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int invertBits( int num)
{
int x = log2(num) + 1;
for ( int i = 0; i < x; i++)
num = (num ^ (1 << i));
return num;
}
int totalPairs( int arr1[], int arr2[], int n, int m)
{
unordered_set< int > s1, s2;
for ( int i = 0; i < m; i++)
s2.insert(arr2[i]);
int count = 0;
for ( int i = 0; i < n; i++) {
if (s1.find(arr1[i]) == s1.end()) {
if (s2.find(invertBits(arr1[i])) != s2.end()) {
count++;
s1.insert(arr1[i]);
}
}
}
return count;
}
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;
}
|
Java
import java.util.*;
import java.io.*;
import java.lang.*;
class GFG
{
static int log2( int N)
{
int result = ( int )(Math.log(N) / Math.log( 2 ));
return result;
}
static int invertBits( int num)
{
int x = log2(num) + 1 ;
for ( int i = 0 ; i < x; i++)
num = (num ^ ( 1 << i));
return num;
}
static int totalPairs( int arr1[], int arr2[], int n, int m)
{
HashSet<Integer> s1 = new HashSet<Integer>();
HashSet<Integer> s2 = new HashSet<Integer>();
for ( int i = 0 ; i < m; i++)
s2.add(arr2[i]);
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (!s1.contains(arr1[i]))
{
if (s2.contains(invertBits(arr1[i])))
{
count++;
s1.add(arr1[i]);
}
}
}
return count;
}
public static void main(String[] args)
{
int arr1[] = { 43 , 7 , 1 , 99 };
int arr2[] = { 5 , 1 , 28 , 20 };
int n = arr1.length;
int m = arr2.length;
System.out.println(totalPairs(arr1, arr2, n, m));
}
}
|
Python3
from math import log2;
def invertBits(num) :
x = log2(num) + 1 ;
for i in range ( int (x)) :
num = (num ^ ( 1 << i));
return num;
def totalPairs(arr1, arr2, n, m) :
s1, s2 = set (), set ();
for i in range (m) :
s2.add(arr2[i]);
count = 0 ;
for i in range (n) :
if arr1[i] not in s1 :
if invertBits(arr1[i]) in s2 :
count + = 1 ;
s1.add(arr1[i]);
return count;
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));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int log2( int N)
{
int result = ( int )(Math.Log(N) / Math.Log(2));
return result;
}
static int invertBits( int num)
{
int x = log2(num) + 1;
for ( int i = 0; i < x; i++)
num = (num ^ (1 << i));
return num;
}
static int totalPairs( int []arr1, int []arr2, int n, int m)
{
HashSet< int > s1 = new HashSet< int >();
HashSet< int > s2 = new HashSet< int >();
for ( int i = 0; i < m; i++)
s2.Add(arr2[i]);
int count = 0;
for ( int i = 0; i < n; i++)
{
if (!s1.Contains(arr1[i]))
{
if (s2.Contains(invertBits(arr1[i])))
{
count++;
s1.Add(arr1[i]);
}
}
}
return count;
}
public static void Main()
{
int []arr1 = { 43, 7, 1, 99 };
int []arr2 = { 5, 1, 28, 20 };
int n = arr1.Length;
int m = arr2.Length;
Console.Write(totalPairs(arr1, arr2, n, m));
}
}
|
Javascript
<script>
function invertBits(num)
{
var x = parseInt(Math.log2(num)) + 1;
for ( var i = 0; i < x; i++)
num = (num ^ (1 << i));
return num;
}
function totalPairs(arr1, arr2, n, m)
{
var s1 = new Set();
var s2 = new Set();
for ( var i = 0; i < m; i++)
s2.add(arr2[i]);
var count = 0;
for ( var i = 0; i < n; i++) {
if (!s1.has(arr1[i]))
{
if (s2.has(invertBits(arr1[i]))) {
count++;
s1.add(arr1[i]);
}
}
}
return count;
}
var arr1 = [43, 7, 1, 99];
var arr2 = [5, 1, 28, 20];
var n = arr1.length;
var m = arr2.length;
document.write( totalPairs(arr1, arr2, n, m));
</script>
|
Time Complexity: O(n+m)
Auxiliary Space: O(n+m)
Share your thoughts in the comments
Please Login to comment...