Count pairs from given array with Bitwise OR equal to K
Last Updated :
28 Feb, 2022
Given an array arr[] consisting of N positive integers and an integer K, the task is to count all pairs possible from the given array with Bitwise OR equal to K.
Examples:
Input: arr[] = {2, 38, 44, 29, 62}, K = 46
Output: 2
Explanation: Only the following two pairs are present in the array whose Bitwise OR is 46:
- 2 OR 44 = 46
- 38 OR 44 = 46
Input: arr[] = {1, 5, 20, 15, 14}, K = 20
Output: 5
Explanation:
There are only 5 pairs whose Bitwise OR is 20:
- 1 OR 15 = 15
- 1 OR 14 = 15
- 5 OR 15 = 15
- 5 OR 14 = 15
- 15 OR 14 = 15
Approach: To solve the problem, the idea is to generate all possible pairs from the given array and count those pairs whose Bitwise OR is equal to K. After checking for all the pairs, print the count stored.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <iostream>
using namespace std;
// Function that counts the pairs from
// the array whose Bitwise OR is K
void countPairs(int arr[], int k, int size)
{
// Stores the required
// count of pairs
int count = 0, x;
// Generate all possible pairs
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
// Perform OR operation
x = arr[i] | arr[j];
// If Bitwise OR is equal
// to K, increment count
if (x == k)
count++;
}
}
// Print the total count
cout << count;
}
// Driver Code
int main()
{
int arr[] = { 2, 38, 44, 29, 62 };
int K = 46;
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
countPairs(arr, K, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function that counts the pairs from
// the array whose Bitwise OR is K
static void countPairs(int[] arr, int k,
int size)
{
// Stores the required
// count of pairs
int count = 0, x;
// Generate all possible pairs
for(int i = 0; i < size - 1; i++)
{
for(int j = i + 1; j < size; j++)
{
// Perform OR operation
x = arr[i] | arr[j];
// If Bitwise OR is equal
// to K, increment count
if (x == k)
count++;
}
}
// Print the total count
System.out.println(count);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 2, 38, 44, 29, 62 };
int K = 46;
int N = arr.length;
// Function Call
countPairs(arr, K, N);
}
}
// This code is contributed by code_hunt
Python3
# Python3 program for the above approach
# Function that counts the pairs from
# the array whose Bitwise OR is K
def countPairs(arr, k, size):
# Stores the required
# count of pairs
count = 0
# Generate all possible pairs
for i in range(size - 1):
for j in range(i + 1, size):
# Perform OR operation
x = arr[i] | arr[j]
# If Bitwise OR is equal
# to K, increment count
if (x == k):
count += 1
# Print the total count
print(count)
# Driver Code
arr = [ 2, 38, 44, 29, 62 ]
K = 46
N = len(arr)
# Function Call
countPairs(arr, K, N)
# This code is contributed by sanjoy_62
C#
// C# program for the above approach
using System;
class GFG{
// Function that counts the pairs from
// the array whose Bitwise OR is K
static void countPairs(int[] arr, int k,
int size)
{
// Stores the required
// count of pairs
int count = 0, x;
// Generate all possible pairs
for(int i = 0; i < size - 1; i++)
{
for(int j = i + 1; j < size; j++)
{
// Perform OR operation
x = arr[i] | arr[j];
// If Bitwise OR is equal
// to K, increment count
if (x == k)
count++;
}
}
// Print the total count
Console.WriteLine(count);
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 38, 44, 29, 62 };
int K = 46;
int N = arr.Length;
// Function Call
countPairs(arr, K, N);
}
}
// This code is contributed by susmitakundugoaldanga
JavaScript
<script>
// JavaScript program for the above approach
// Function that counts the pairs from
// the array whose Bitwise OR is K
function countPairs(arr, k, size)
{
// Stores the required
// count of pairs
let count = 0, x;
// Generate all possible pairs
for(let i = 0; i < size - 1; i++)
{
for(let j = i + 1; j < size; j++)
{
// Perform OR operation
x = arr[i] | arr[j];
// If Bitwise OR is equal
// to K, increment count
if (x == k)
count++;
}
}
// Print the total count
document.write(count);
}
// Driver Code
let arr = [ 2, 38, 44, 29, 62 ];
let K = 46;
let N = arr.length;
// Function Call
countPairs(arr, K, N);
// This code is contributed by avijitmondal998.
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
Count all triplets from given Array whose bitwise XOR is equal to K Given an array arr[] which contains N positive integers and an integer K. The task is to count all triplets whose XOR is equal to the K. i.e arr[ i ] ^ arr[ j ] ^ arr[ k ] = X where 0 ⤠i < j < k < N ( 0 based indexing) Examples: Input: arr[] = { 2, 1, 3, 7, 5, 4}, K = 5Output: 2Explanation
15+ min read
Count pairs having Bitwise XOR less than K from given array 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
15+ min read
Count of Subarrays with sum equals k in given Binary Array Given a binary array arr[] and an integer k, the task is to find the count of non-empty subarrays with a sum equal to k.Examples:Input: arr[] = {1, 0, 1, 1, 0, 1}, k = 2Output: 6Explanation: All valid subarrays are: {1, 0, 1}, {0, 1, 1}, {1, 1}, {1, 0, 1}, {0, 1, 1, 0}, {1, 1, 0}.Input: arr[] = {0,
10 min read
Count all pairs of an array which differ in K bits Given an array of size n and integer k, count all pairs in array which differ in exactly K bits of binary representation of both the numbers.The input arrays have elements with small values and possibly many repetitions. Examples: Input: arr[] = {2, 4, 1, 3, 1} k = 2 Output: 5 Explanation: There are
14 min read
Count pairs having bitwise XOR greater than K from a given array 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 conditio
15+ min read
Count values whose Bitwise OR with A is equal to B Given two integers A and B, the task is to count possible values of X that satisfies the condition A | X = B. Note: | represents Bitwise OR operation. Examples: Input: A = 2, B = 3Output: 2Explanation: Since, 2 | 1 = 3 and 2 | 3 = 3. Therefore, the possible values of x are 1 and 3. Input: A = 5, B =
6 min read