Count of pairs whose bitwise AND is a power of 2 - GeeksforGeeks

04 May, 2021
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++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to check if x is power of 2bool check(int x){    // Returns true if x is a power of 2    return x && (!(x & (x - 1)));} // Function to return the// number of valid pairsint 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 Codeint 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 program for the above approachclass GFG{ // Method to check if x is power of 2static 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 pairsstatic 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 Codepublic 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 program for the above approach # Function to check if x is power of 2def 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 pairsdef 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 arrayarr = [ 6, 4, 2, 3 ]n = len(arr) # Function Callprint(count(arr, n)) # This code is contributed by divyeshrabadiya07
// C# program for the above approachusing System;class GFG{ // Method to check if x is power of 2static 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 pairsstatic 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 Codepublic 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
<script> // JavaScript program to implement// the above approach // Method to check if x is power of 2function check(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 pairsfunction count(arr, n){    let cnt = 0;       // Iterate for all possible pairs    for(let i = 0; i < n - 1; i++)    {       for(let 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    // Given array arr[]    let arr = [ 6, 4, 2, 3 ];       let n = arr.length;           // Function call    document.write(count(arr, n)); // This code is contributed by susmitakundugoaldanga.</script>
4
 
Time Complexity: O(N2) Auxiliary Space: O(1) 
dewantipandeydp
Code_Mech
divyeshrabadiya07
susmitakundugoaldanga
Bitwise-AND
maths-power
Arrays
Bit Magic
Competitive Programming
Mathematical
Arrays
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
How to swap two numbers without using a temporary variable?
Little and Big Endian Mystery