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 greater than K.
Examples:
Input: arr = {1, 2, 3, 5} , K = 2
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[3] = 1 ^ 5 = 4
arr[1] ^ arr[3] = 2 ^ 5 = 7
arr[0] ^ arr[3] = 3 ^ 5 = 6
Therefore, the required output is 4.
Input: arr[] = {3, 5, 6,8}, K = 2
Output: 6
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 greater than K or not. If found to be true, then increment the count of pairs having bitwise XOR greater than K. Finally, print the count of such pairs obtained.
C++
#include <bits/stdc++.h>
using namespace std;
int cntGreaterPairs( int arr[], int n, int k)
{
int count=0;
for ( int i=0;i<n;i++)
{
for ( int j=i+1;j<n;j++)
{
if (arr[i]^arr[j]>k)
count++;
}
}
return count;
}
int main()
{
int arr[] = {3, 5, 6, 8};
int K= 2;
int N = sizeof (arr) / sizeof (arr[0]);
cout<<cntGreaterPairs(arr, N, K);
}
|
Java
import java.util.*;
class Main {
static int cntGreaterPairs( int arr[], int n, int k)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
if ((arr[i] ^ arr[j]) > k)
count++;
}
}
return count;
}
public static void main(String args[])
{
int arr[] = { 3 , 5 , 6 , 8 };
int K = 2 ;
int N = arr.length;
System.out.print(cntGreaterPairs(arr, N, K));
}
}
|
Python3
def cntGreaterPairs(arr, n, k):
count = 0
for i in range (n):
for j in range (i + 1 , n):
if arr[i]^arr[j] > k:
count + = 1
return count
arr = [ 3 , 5 , 6 , 8 ]
K = 2
N = len (arr)
print (cntGreaterPairs(arr, N, K))
|
Javascript
function cntGreaterPairs(arr, n, k) {
let count = 0;
for (let i = 0; i < n; i++) {
for (let j = i+1; j < n; j++) {
if ((arr[i]^arr[j]) > k) {
count++;
}
}
}
return count;
}
const arr = [3, 5, 6, 8];
const K = 2;
const N = arr.length;
console.log(cntGreaterPairs(arr, N, K));
|
C#
using System;
public class Program
{
public static int cntGreaterPairs( int [] arr, int n, int k)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
if ((arr[i] ^ arr[j]) > k)
count++;
}
}
return count;
}
public static void Main()
{
int [] arr = { 3, 5, 6, 8 };
int K = 2;
int N = arr.Length;
Console.WriteLine(cntGreaterPairs(arr, N, K));
}
}
|
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 greater than K and insert the binary representation of the current element into the Trie. Finally, print the count of pairs having bitwise XOR greater 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 cntGreater(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) {
root =
root->child[1 - x];
}
else {
if (root->child[1 - x]) {
cntPairs +=
root->child[1 - x]->cnt;
}
root = root->child[x];
}
}
return cntPairs;
}
int cntGreaterPairs( int arr[], int N,
int K) {
TrieNode *root = new TrieNode();
int cntPairs = 0;
for ( int i = 0;i < N; i++){
cntPairs += cntGreater(root,
arr[i], K);
insertTrie(root, arr[i]);
}
return cntPairs;
}
int main()
{
int arr[] = {3, 5, 6, 8};
int K= 2;
int N = sizeof (arr) / sizeof (arr[0]);
cout<<cntGreaterPairs(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] != null )
root.child[x].cnt += 1 ;
if (x < 2 )
root = root.child[x];
}
}
static int cntGreater(TrieNode root,
int N, int K)
{
int cntPairs = 1 ;
for ( int i = 31 ; i >= 0 &&
root!= null ; i--)
{
int x = N & ( 1 << i);
int y = K & ( 1 << i);
if (y == 1 )
{
root = root.child[ 1 - x];
}
else
{
if (x < 2 &&
root.child[ 1 - x] != null )
{
cntPairs += root.child[ 1 - x].cnt;
}
if (x < 2 )
root = root.child[x];
}
}
return cntPairs;
}
static int cntGreaterPairs( int arr[],
int N, int K)
{
TrieNode root = new TrieNode();
int cntPairs = 0 ;
for ( int i = 0 ; i < N; i++)
{
cntPairs += cntGreater(root,
arr[i], K);
insertTrie(root, arr[i]);
}
return cntPairs;
}
public static void main(String[] args)
{
int arr[] = { 3 , 5 , 6 , 8 };
int K = 2 ;
int N = arr.length;
System.out.print(cntGreaterPairs(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 cntGreater(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 ):
root = root.child[ 1 - x]
else :
if (root.child[ 1 - x]):
cntPairs + = root.child[ 1 - x].cnt
root = root.child[x]
return cntPairs
def cntGreaterPairs(arr, N, K):
root = TrieNode()
cntPairs = 0
for i in range (N):
cntPairs + = cntGreater(root, arr[i], K)
insertTrie(root, arr[i])
return cntPairs
if __name__ = = '__main__' :
arr = [ 3 , 5 , 6 , 8 ]
K = 2
N = len (arr)
print (cntGreaterPairs(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] != null )
root.child[x].cnt += 1;
if (x < 2)
root = root.child[x];
}
}
static int cntGreater(TrieNode root,
int N, int K)
{
int cntPairs = 1;
for ( int i = 31; i >= 0 &&
root != null ; i--)
{
int x = N & (1 << i);
int y = K & (1 << i);
if (y == 1)
{
root = root.child[1 - x];
}
else
{
if (x < 2 &&
root.child[1 - x] != null )
{
cntPairs += root.child[1 - x].cnt;
}
if (x < 2)
root = root.child[x];
}
}
return cntPairs;
}
static int cntGreaterPairs( int []arr,
int N, int K)
{
TrieNode root = new TrieNode();
int cntPairs = 0;
for ( int i = 0; i < N; i++)
{
cntPairs += cntGreater(root,
arr[i], K);
insertTrie(root, arr[i]);
}
return cntPairs;
}
public static void Main(String[] args)
{
int []arr = { 3, 5, 6, 8 };
int K = 2;
int N = arr.Length;
Console.Write(cntGreaterPairs(arr,
N, K));
}
}
|
Javascript
<script>
class TrieNode
{
constructor()
{
this .child = new Array(2);
this .child[0] = this .child[1] = null ;
this .cnt = 0;
}
}
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] != null )
root.child[x].cnt += 1;
if (x < 2)
root = root.child[x];
}
}
function cntGreater(root, N, K)
{
let cntPairs = 1;
for (let i = 31; i >= 0 &&
root!= null ; i--)
{
let x = N & (1 << i);
let y = K & (1 << i);
if (y == 1)
{
root = root.child[1 - x];
}
else
{
if (x < 2 &&
root.child[1 - x] != null )
{
cntPairs += root.child[1 - x].cnt;
}
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 += cntGreater(root,
arr[i], K);
insertTrie(root, arr[i]);
}
return cntPairs;
}
let arr=[3, 5, 6, 8];
let K = 2;
let N = arr.length;
document.write(cntGreaterPairs(arr,N, K));
</script>
|
Time Complexity:O(N * 32)
Auxiliary Space:O(N * 32)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Mar, 2023
Like Article
Save Article