Count pairs in Array such that their bitwise XOR has Kth right bit set
Last Updated :
25 Sep, 2023
Given an array arr[] of size N and an integer K, the task is to find the number of pairs in the array such that the Kth bit from the right of their bitwise XOR is set.
Examples:
Input: arr[] = {3, 13, 2, 9}, N = 4, K = 3
Output: 4
Explanation: The pairs that have the kth bit set in bitwise xor value are {3, 13}, {3, 9}, {2, 13}, {2, 9}
Input: arr[] = {7, 6, 5, 3}, N = 4, K = 2
Output: 3
Explanation: The pairs that have a kth bit set in bitwise xor value are {7, 3}, {6, 3}, {5, 3}
Naive Approach: The basic way to solve the problem is as follows:
The most basic way to solve this problem is to consider each possible pair of elements from the array and check whether the number formed from the bitwise xor of both elements has a kth bit set.
C++
#include <iostream>
using namespace std;
int countPairs( int arr[], int n, int k) {
int count = 0;
for ( int i = 0; i < n-1; i++) {
for ( int j = i+1; j < n; j++) {
if ((arr[i]^arr[j]) & (1 << (k))) {
count++;
}
}
}
return count;
}
int main() {
int arr1[] = { 3, 13, 2, 9 };
int K = 1;
int N = sizeof (arr1) / sizeof (arr1[0]);
cout << countPairs(arr1, N, K) << endl;
int arr2[] = { 7, 6, 5, 3 };
K = 2;
N = sizeof (arr2) / sizeof (arr2[0]);
cout << countPairs(arr2, N, K) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
public static int countPairs( int [] arr, int n, int k) {
int count = 0 ;
for ( int i = 0 ; i < n- 1 ; i++) {
for ( int j = i+ 1 ; j < n; j++) {
if (((arr[i]^arr[j]) & ( 1 << (k))) != 0 ) {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
int [] arr1 = { 3 , 13 , 2 , 9 };
int K = 1 ;
int N = arr1.length;
System.out.println(countPairs(arr1, N, K));
int [] arr2 = { 7 , 6 , 5 , 3 };
K = 2 ;
N = arr2.length;
System.out.println(countPairs(arr2, N, K));
}
}
|
Python3
def countPairs(arr, n, k):
count = 0
for i in range (n - 1 ):
for j in range (i + 1 , n):
if (arr[i]^arr[j]) & ( 1 << k):
count + = 1
return count
arr1 = [ 3 , 13 , 2 , 9 ]
K = 1
N = len (arr1)
print (countPairs(arr1, N, K))
arr2 = [ 7 , 6 , 5 , 3 ]
K = 2
N = len (arr2)
print (countPairs(arr2, N, K))
|
C#
using System;
public class GFG {
public static int countPairs( int [] arr, int n, int k) {
int count = 0;
for ( int i = 0; i < n-1; i++) {
for ( int j = i+1; j < n; j++) {
if (((arr[i]^arr[j]) & (1 << (k))) != 0) {
count++;
}
}
}
return count;
}
public static void Main( string [] args) {
int [] arr1 = { 3, 13, 2, 9 };
int K = 1;
int N = arr1.Length;
Console.WriteLine(countPairs(arr1, N, K));
int [] arr2 = { 7, 6, 5, 3 };
K = 2;
N = arr2.Length;
Console.WriteLine(countPairs(arr2, N, K));
}
}
|
Javascript
function countPairs(arr, n, k) {
let count = 0;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
if ((arr[i] ^ arr[j]) & (1 << k)) {
count += 1;
}
}
}
return count;
}
const arr1 = [3, 13, 2, 9];
const K = 1;
const N1 = arr1.length;
console.log(countPairs(arr1, N1, K));
const arr2 = [7, 6, 5, 3];
const K1 = 2;
const N = arr2.length;
console.log(countPairs(arr2, N, K1));
|
Time Complexity: O(N2 * log(Max_ele)) where Max_ele is the largest element of the array.
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following idea:
Consider two numbers X and Y where X is having its Kth bit set. Only the following two scenarios can happen :
- If Y also has Kth bit set then X^Y will have Kth bit unset.
- If the Kth bit of Y is unset then X^Y will have the Kth bit set.
Follow the below-mentioned steps to implement the above approach:
- Count the number of elements having Kth right bit set.
- For each element arr[i] we know if it has Kth right bit set or not.
- If the Kth right bit is set in arr[i] then take a count of all those elements that have bit unset at the Kth position from right.
- If the Kth right bit is unset in arr[i] then consider all those elements that have set the bit at the Kth position from right.
- Ultimately, divide the final answer by 2 as the calculation considers each pair twice.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int arr[], int n, int k)
{
int bits[32];
memset (bits, 0, sizeof (bits));
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < 32; j++) {
if ((arr[i] & (1 << j)) > 0) {
bits[j]++;
}
}
}
int ans = 0;
for ( int i = 0; i < n; i++) {
if ((arr[i] & (1 << k)) > 0) {
ans += (n - bits[k]);
}
else {
ans += (bits[k]);
}
}
ans = ans / 2;
return ans;
}
int main()
{
int arr1[] = { 3, 13, 2, 9 };
int K = 1;
int N = sizeof (arr1) / sizeof (arr1[0]);
cout << solve(arr1, N, K) << endl;
int arr2[] = { 7, 6, 5, 3 };
K = 2;
N = sizeof (arr2) / sizeof (arr2[0]);
cout << solve(arr2, N, K) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static int solve( int [] arr, int n, int k) {
int [] bits = new int [ 32 ];
Arrays.fill(bits, 0 );
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < 32 ; j++) {
if ((arr[i] & ( 1 << j)) > 0 ) {
bits[j]++;
}
}
}
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
if ((arr[i] & ( 1 << k)) > 0 ) {
ans += (n - bits[k]);
}
else {
ans += (bits[k]);
}
}
ans = ans / 2 ;
return ans;
}
public static void main(String[] args) {
int [] arr1 = { 3 , 13 , 2 , 9 };
int K = 1 ;
int N = arr1.length;
System.out.println(solve(arr1, N, K));
int [] arr2 = { 7 , 6 , 5 , 3 };
K = 2 ;
N = arr2.length;
System.out.println(solve(arr2, N, K));
}
}
|
Python3
def solve(arr, n, k):
bits = [ 0 ] * 32
for i in range (n):
for j in range ( 32 ):
if arr[i] & ( 1 << j) > 0 :
bits[j] + = 1
ans = 0
for i in range (n):
if arr[i] & ( 1 << k) > 0 :
ans + = (n - bits[k])
else :
ans + = bits[k]
ans = ans / / 2
return ans
arr1 = [ 3 , 13 , 2 , 9 ]
K = 1
N = len (arr1)
print (solve(arr1, N, K))
arr2 = [ 7 , 6 , 5 , 3 ]
K = 2
N = len (arr2)
print (solve(arr2, N, K))
|
C#
using System;
class Gfg{
static int solve( int [] arr, int n, int k)
{
int [] bits= new int [32];
for ( int i=0; i<32; i++)
bits[i]=0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < 32; j++) {
if ((arr[i] & (1 << j)) > 0) {
bits[j]++;
}
}
}
int ans = 0;
for ( int i = 0; i < n; i++) {
if ((arr[i] & (1 << k)) > 0) {
ans += (n - bits[k]);
}
else {
ans += (bits[k]);
}
}
ans = ans / 2;
return ans;
}
public static void Main()
{
int [] arr1 = { 3, 13, 2, 9 };
int K = 1;
int N = arr1.Length;
Console.WriteLine(solve(arr1, N, K));
int [] arr2 = { 7, 6, 5, 3 };
K = 2;
N = arr2.Length;
Console.WriteLine(solve(arr2, N, K));
}
}
|
Javascript
function solve( arr, n, k)
{
let bits= new Array(32).fill(0);
for (let i = 0; i < n; i++) {
for (let j = 0; j < 32; j++) {
if ((arr[i] & (1 << j)) > 0) {
bits[j]++;
}
}
}
let ans = 0;
for (let i = 0; i < n; i++) {
if ((arr[i] & (1 << k)) > 0) {
ans += (n - bits[k]);
}
else {
ans += (bits[k]);
}
}
ans = Math.floor(ans / 2);
return ans;
}
let arr1 = [ 3, 13, 2, 9 ];
let K = 1;
let N = arr1.length;
console.log(solve(arr1, N, K));
let arr2 = [ 7, 6, 5, 3 ];
K = 2;
N = arr2.length;
console.log(solve(arr2, N, K));
|
Time Complexity: O(N * log(Max_ele)) where Max_ele is the maximum value in the array
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...