Count pairs from given array with Bitwise OR equal to K
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> |
Output:
2
Time Complexity: O(N2)
Auxiliary Space: O(1)