Given an array arr[] of N positive integers. The task is to find the number of pairs whose Bitwise AND value is a power of 2.
Examples:
Input: arr[] = {2, 1, 3, 4}
Output: 2
Explanation:
There are 2 pairs (2, 3) and (1, 3) in this array whose Bitwise AND values are:
1. (2 & 3) = 1 = (20)
2. (1 & 3) = 1 = (20).Input: arr[] = {6, 4, 2, 3}
Output: 4
Explanation:
There are 4 pairs (6, 4), (6, 2), (6, 3), (2, 3) whose Bitwise and is power of 2.
Approach: For each possible pair in the given array, the idea to check whether Bitwise AND of each pairs of elements is perfect power of 2 or not. If “Yes” then count this pair Else check for the next pair.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if x is power of 2 bool check(int x) { // Returns true if x is a power of 2 return x && (!(x & (x - 1))); } // Function to return the // number of valid pairs int count(int arr[], int n) { int cnt = 0; // Iterate for all possible pairs for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { // Bitwise and value of // the pair is passed if (check(arr[i] & arr[j])) cnt++; } } // Return the final count return cnt; } // Driver Code int main() { // Given array int arr[] = { 6, 4, 2, 3 }; int n = sizeof(arr) / sizeof(arr[0]); // Function Call cout << count(arr, n); return 0; } |
Java
// Java program for the above approach class GFG{ // Method to check if x is power of 2 static boolean check(int x) { // First x in the below expression // is for the case when x is 0 return x != 0 && ((x & (x - 1)) == 0); } // Function to return the // number of valid pairs static int count(int arr[], int n) { int cnt = 0; // Iterate for all possible pairs for(int i = 0; i < n - 1; i++) { for(int j = i + 1; j < n; j++) { // Bitwise and value of // the pair is passed if (check(arr[i] & arr[j])) cnt++; } } // Return the final count return cnt; } // Driver Code public static void main(String[] args) { // Given array arr[] int arr[] = new int[]{ 6, 4, 2, 3 }; int n = arr.length; // Function call System.out.print(count(arr, n)); } } // This code is contributed by Pratima Pandey |
Python3
# Python3 program for the above approach # Function to check if x is power of 2 def check(x): # Returns true if x is a power of 2 return x and (not(x & (x - 1))) # Function to return the # number of valid pairs def count(arr, n): cnt = 0 # Iterate for all possible pairs for i in range(n - 1): for j in range(i + 1, n): # Bitwise and value of # the pair is passed if check(arr[i] & arr[j]): cnt = cnt + 1 # Return the final count return cnt # Given array arr = [ 6, 4, 2, 3 ] n = len(arr) # Function Call print(count(arr, n)) # This code is contributed by divyeshrabadiya07 |
C#
// C# program for the above approach using System; class GFG{ // Method to check if x is power of 2 static bool check(int x) { // First x in the below expression // is for the case when x is 0 return x != 0 && ((x & (x - 1)) == 0); } // Function to return the // number of valid pairs static int count(int []arr, int n) { int cnt = 0; // Iterate for all possible pairs for(int i = 0; i < n - 1; i++) { for(int j = i + 1; j < n; j++) { // Bitwise and value of // the pair is passed if (check(arr[i] & arr[j])) cnt++; } } // Return the final count return cnt; } // Driver Code public static void Main() { // Given array arr[] int []arr = new int[]{ 6, 4, 2, 3 }; int n = arr.Length; // Function call Console.Write(count(arr, n)); } } // This code is contributed by Code_Mech |
4
Time Complexity: O(N2)
Auxiliary Space: O(1)
Recommended Posts:
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count pairs in Array whose product is a Kth power of any positive integer
- Number of pairs whose sum is a power of 2
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Find the sum of power of bit count raised to the power B
- Count pairs with Bitwise AND as ODD number
- Count pairs with Bitwise-AND as even number
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Count of pairs from Array with sum equal to twice their bitwise AND
- Check if bitwise AND of any subset is power of two
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Count of even and odd power pairs in an Array
- Count of elements which cannot form any pair whose sum is power of 2
- Count pairs with Bitwise OR as Even number
- Count pairs with Bitwise XOR as ODD number
- Count pairs with Bitwise XOR as EVEN number
- Count pairs with bitwise OR less than Max
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


