Skip to content
Related Articles
Count of pairs whose bitwise AND is a power of 2
• Difficulty Level : Easy
• Last Updated : 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:
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:
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 ``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);` `    ``// 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`

## Javascript

 ``
Output:

`4`

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up