Given an array arr[]of size N and an integer K, the task is to count the number of pairs from the given array such that the Bitwise XOR of each pair is less than K.
Examples:
Input: arr = {1, 2, 3, 5} , K = 5
Output: 4
Explanation:
Bitwise XOR of all possible pairs that satisfy the given conditions are:
arr[0] ^ arr[1] = 1 ^ 2 = 3
arr[0] ^ arr[2] = 1 ^ 3 = 2
arr[0] ^ arr[3] = 1 ^ 5 = 4
arr[1] ^ arr[2] = 3 ^ 5 = 1
Therefore, the required output is 4.
Input: arr[] = {3, 5, 6, 8}, K = 7
Output: 3
Naive Approach: The simplest approach to solve this problem is to traverse the given array and generate all possible pairs of the given array and for each pair, check if bitwise XOR of the pair is less than K or not. If found to be true, then increment the count of pairs having bitwise XOR less than K. Finally, print the count of such pairs obtained.
C++
#include <bits/stdc++.h>
using namespace std;
int cntSmallerPairs( int arr[], int n,
int k) {
int cnt = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i+1; j < n; j++) {
if ((arr[i] ^ arr[j]) < k) {
cnt++;
}
}
}
cout << cnt << endl;
}
int main()
{
int arr[] = {3, 5, 6, 8};
int K= 7;
int N = sizeof (arr) / sizeof (arr[0]);
cntSmallerPairs(arr, N, K);
}
|
Java
import java.util.*;
class Main {
static int cntSmallerPairs( int arr[], int n, int k)
{
int cnt = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
if ((arr[i] ^ arr[j]) < k) {
cnt++;
}
}
}
return cnt;
}
public static void main(String[] args)
{
int arr[] = { 3 , 5 , 6 , 8 };
int K = 7 ;
int N = arr.length;
int cnt = cntSmallerPairs(arr, N, K);
System.out.println(cnt);
}
}
|
Python3
def cntSmallerPairs(arr, n, k):
cnt = 0
for i in range (n):
for j in range (i + 1 , n):
if (arr[i] ^ arr[j]) < k:
cnt + = 1
print (cnt)
if __name__ = = '__main__' :
arr = [ 3 , 5 , 6 , 8 ]
K = 7
N = len (arr)
cntSmallerPairs(arr, N, K)
|
C#
using System;
public class Program {
static void cntSmallerPairs( int [] arr, int n, int k)
{
int cnt = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if ((arr[i] ^ arr[j]) < k) {
cnt++;
}
}
}
Console.WriteLine(cnt);
}
public static void Main( string [] args)
{
int [] arr = { 3, 5, 6, 8 };
int K = 7;
int N = arr.Length;
cntSmallerPairs(arr, N, K);
}
}
|
Javascript
function cntSmallerPairs(arr, n, k) {
let cnt = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if ((arr[i] ^ arr[j]) < k) {
cnt++;
}
}
}
return cnt;
}
const arr = [3, 5, 6, 8];
const K = 7;
const N = arr.length;
const cnt = cntSmallerPairs(arr, N, K);
console.log(cnt);
|
Time Complexity:O(N2)
Auxiliary Space:O(1)
Efficient Approach: The problem can be solved using Trie. The idea is to iterate over the given array and for each array element, count the number of elements present in the Trie whose bitwise XOR with the current element is less than K and insert the binary representation of the current element into the Trie. Finally, print the count of pairs having bitwise XOR less than K. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct TrieNode
{
TrieNode *child[2];
int cnt;
TrieNode() {
child[0] = child[1] = NULL;
cnt = 0;
}
};
void insertTrie(TrieNode *root, int N) {
for ( int i = 31; i >= 0; i--) {
bool x = (N) & (1 << i);
if (!root->child[x]) {
root->child[x] = new TrieNode();
}
root->child[x]->cnt+= 1;
root= root->child[x];
}
}
int cntSmaller(TrieNode * root,
int N, int K)
{
int cntPairs = 0;
for ( int i = 31; i >= 0 &&
root; i--) {
bool x = N & (1 << i);
bool y = K & (1 << i);
if (y) {
if (root->child[x]) {
cntPairs +=
root->child[x]->cnt;
}
root =
root->child[1 - x];
}
else {
root = root->child[x];
}
}
return cntPairs;
}
int cntSmallerPairs( int arr[], int N,
int K) {
TrieNode *root = new TrieNode();
int cntPairs = 0;
for ( int i = 0;i < N; i++){
cntPairs += cntSmaller(root,
arr[i], K);
insertTrie(root, arr[i]);
}
return cntPairs;
}
int main()
{
int arr[] = {3, 5, 6, 8};
int K= 7;
int N = sizeof (arr) / sizeof (arr[0]);
cout<<cntSmallerPairs(arr, N, K);
}
|
Java
import java.util.*;
class GFG{
static class TrieNode
{
TrieNode child[] = new TrieNode[ 2 ];
int cnt;
TrieNode()
{
child[ 0 ] = child[ 1 ] = null ;
cnt = 0 ;
}
};
static void insertTrie(TrieNode root,
int N)
{
for ( int i = 31 ; i >= 0 ; i--)
{
int x = (N) & ( 1 << i);
if (x < 2 && root.child[x] ==
null )
{
root.child[x] = new TrieNode();
}
if (x < 2 )
root.child[x].cnt += 1 ;
if (x < 2 )
root = root.child[x];
}
}
static int cntSmaller(TrieNode root,
int N, int K)
{
int cntPairs = 0 ;
for ( int i = 31 ; i >= 0 &&
root != null ; i--)
{
int x = (N & ( 1 << i));
int y = (K & ( 1 << i));
if (y == 1 )
{
if (root.child[x] != null )
{
cntPairs +=
root.child[x].cnt;
}
root = root.child[ 1 - x];
}
else
{
if (x < 2 )
root = root.child[x];
}
}
return cntPairs;
}
static int cntSmallerPairs( int arr[],
int N, int K)
{
TrieNode root = new TrieNode();
int cntPairs = 0 ;
for ( int i = 0 ; i < N; i++)
{
cntPairs += cntSmaller(root,
arr[i], K);
insertTrie(root, arr[i]);
}
return cntPairs;
}
public static void main(String[] args)
{
int arr[] = { 3 , 5 , 6 , 8 };
int K= 7 ;
int N = arr.length;
System.out.print(cntSmallerPairs(arr,
N, K));
}
}
|
Python3
class TrieNode:
def __init__( self ):
self .child = [ None , None ]
self .cnt = 0
def insertTrie(root, N):
for i in range ( 31 , - 1 , - 1 ):
x = bool ( (N) & ( 1 << i));
if (root.child[x] = = None ):
root.child[x] = TrieNode();
root.child[x].cnt + = 1 ;
root = root.child[x];
def cntSmaller(root, N, K):
cntPairs = 0 ;
for i in range ( 31 , - 1 , - 1 ):
if (root = = None ):
break
x = bool (N & ( 1 << i))
y = K & ( 1 << i);
if (y ! = 0 ):
if (root.child[x]):
cntPairs + = root.child[ x].cnt
root = root.child[ 1 - x];
else :
root = root.child[x]
return cntPairs;
def cntSmallerPairs(arr, N, K):
root = TrieNode();
cntPairs = 0 ;
for i in range (N):
cntPairs + = cntSmaller(root, arr[i], K);
insertTrie(root, arr[i]);
return cntPairs;
if __name__ = = '__main__' :
arr = [ 3 , 5 , 6 , 8 ]
K = 7 ;
N = len (arr)
print (cntSmallerPairs(arr, N, K))
|
C#
using System;
class GFG{
public class TrieNode
{
public TrieNode []child = new TrieNode[2];
public int cnt;
public TrieNode()
{
child[0] = child[1] = null ;
cnt = 0;
}
};
static void insertTrie(TrieNode root,
int N)
{
for ( int i = 31; i >= 0; i--)
{
int x = (N) & (1 << i);
if (x < 2 && root.child[x] == null )
{
root.child[x] = new TrieNode();
}
if (x < 2)
root.child[x].cnt += 1;
if (x < 2)
root = root.child[x];
}
}
static int cntSmaller(TrieNode root,
int N, int K)
{
int cntPairs = 0;
for ( int i = 31; i >= 0 &&
root != null ; i--)
{
int x = (N & (1 << i));
int y = (K & (1 << i));
if (y == 1)
{
if (root.child[x] != null )
{
cntPairs += root.child[x].cnt;
}
root = root.child[1 - x];
}
else
{
if (x < 2)
root = root.child[x];
}
}
return cntPairs;
}
static int cntSmallerPairs( int []arr,
int N, int K)
{
TrieNode root = new TrieNode();
int cntPairs = 0;
for ( int i = 0; i < N; i++)
{
cntPairs += cntSmaller(root,
arr[i], K);
insertTrie(root, arr[i]);
}
return cntPairs;
}
public static void Main(String[] args)
{
int []arr = { 3, 5, 6, 8 };
int K= 7;
int N = arr.Length;
Console.Write(cntSmallerPairs(arr,
N, K));
}
}
|
Javascript
class TrieNode
{
constructor()
{
this .child = new Array(2);
this .cnt = 0;
this .child[0] = this .child[1] = null ;
}
}
function insertTrie(root,N)
{
for (let i = 31; i >= 0; i--)
{
let x = (N) & (1 << i);
if (x < 2 && root.child[x] == null )
{
root.child[x] = new TrieNode();
}
if (x < 2 )
root.child[x].cnt += 1;
if (x < 2 )
root = root.child[x];
}
}
function cntSmaller(root, N, K)
{
let cntPairs = 0;
for (let i = 31; i >= 0 &&
root!= null ; i--)
{
let x = N & (1 << i);
let y = K & (1 << i);
if (y)
{
if (root.child[x] != null )
{
cntPairs += root.child[x].cnt;
}
root = root.child[1-x];
}
else
{
if (x < 2)
root = root.child[x];
}
}
return cntPairs;
}
function cntGreaterPairs(arr,N,K)
{
let root = new TrieNode();
let cntPairs = 0;
for (let i = 0; i < N; i++)
{
cntPairs += cntSmaller(root,
arr[i], K);
insertTrie(root, arr[i]);
}
return cntPairs;
}
let arr=[3, 5, 6, 8];
let K = 7;
let N = arr.length;
console.log(cntGreaterPairs(arr,N, K));
|
Time Complexity:O(N * 32)
Auxiliary Space:O(N * 32)