Count of pairs whose bitwise AND is a power of 2

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output:
4

Time Complexity: O(N2)
Auxiliary Space: O(1)





Check out this Author's contributed articles.

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.



Article Tags :