# Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two

Given an array of N elements. The task is to count **unordered pairs** (i, j) in the array such that the product of a[i] and a[j] can be expressed as a power of two.

**Examples**:

Input: arr[] = {2, 3, 4, 8, 10}Output: 3Explanation:The pair of array element will be (2, 4), (2, 8), (4, 8) whose product are 8, 16, 32 respectively which can be expressed as power of 2, like 2^3, 2^4, 2^5.Input: arr[] = { 2, 5, 8, 16, 128 }Output: 6

If you multiply and and their product become , then z=x*y, now if it’s possible to express as power of two then it can be proved that both and can be expressed as power of two. Basically **z= 2 ^{a} = 2^{(b+c)} = 2^{b} * 2^{c} = x * y**, where and both

can hold a minimum value 0.

So now we have to count the number of elements in the array which can be expressed as a power of two. If the count is k, then answer will be kC2 = k*(k-1)/2, as we need the count of unordered pairs.

Below is the implementation of above approach:

## C++

`// C++ program to Count unordered pairs (i, j) ` `// in array such that product of a[i] and a[j] ` `// can be expressed as power of two ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* Function to check if x is power of 2*/` `bool` `isPowerOfTwo(` `int` `x) ` `{ ` ` ` `/* First x in the below expression is ` ` ` `for the case when x is 0 */` ` ` `return` `x && (!(x&(x-1))); ` `} ` ` ` `// Function to Count unordered pairs ` `void` `Count_pairs(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// is a number can be expressed ` ` ` `// as power of two ` ` ` `if` `(isPowerOfTwo(a[i])) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// count total number ` ` ` `// of unordered pairs ` ` ` `int` `ans = (count * (count - 1)) / 2; ` ` ` ` ` `cout << ans << ` `"\n"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 2, 5, 8, 16, 128 }; ` ` ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `Count_pairs(a, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to Count unordered pairs (i, j) ` `// in array such that product of a[i] and a[j] ` `// can be expressed as power of two ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `/* Function to check if x is power of 2*/` `static` `boolean` `isPowerOfTwo(` `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 Count unordered pairs ` `static` `void` `Count_pairs(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` ` ` `// is a number can be expressed ` ` ` `// as power of two ` ` ` `if` `(isPowerOfTwo(a[i])) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// count total number ` ` ` `// of unordered pairs ` ` ` `int` `ans = (count * (count - ` `1` `)) / ` `2` `; ` ` ` ` ` `System.out.println( ans); ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `a[] = { ` `2` `, ` `5` `, ` `8` `, ` `16` `, ` `128` `}; ` ` ` ` ` `int` `n = a.length; ` ` ` `Count_pairs(a, n); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed ` `// by shs ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 program to Count unordered pairs ` `# (i, j) in array such that product of a[i] ` `# and a[j] can be expressed as power of two ` ` ` `# Function to check if x is power of 2 ` `def` `isPowerOfTwo(x) : ` ` ` ` ` `# First x in the below expression ` ` ` `# is for the case when x is 0 ` ` ` `return` `(x ` `and` `(` `not` `(x & (x ` `-` `1` `)))) ` ` ` `# Function to Count unordered pairs ` `def` `Count_pairs(a, n) : ` ` ` ` ` `count ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` ` ` `# is a number can be expressed ` ` ` `# as power of two ` ` ` `if` `isPowerOfTwo(a[i]) : ` ` ` `count ` `+` `=` `1` ` ` ` ` `# count total number ` ` ` `# of unordered pairs ` ` ` `ans ` `=` `(count ` `*` `(count ` `-` `1` `)) ` `/` `2` ` ` ` ` `print` `(ans) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[ ` `2` `, ` `5` `, ` `8` `, ` `16` `, ` `128` `] ` ` ` ` ` `n ` `=` `len` `(a) ` ` ` ` ` `Count_pairs(a, n) ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to Count unordered pairs (i, j) ` `// in array such that product of a[i] and a[j] ` `// can be expressed as power of two ` ` ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` ` ` `/* Function to check if x is power of 2*/` `static` `bool` `isPowerOfTwo(` `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 Count unordered pairs ` `static` `void` `Count_pairs(` `int` `[]a, ` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// is a number can be expressed ` ` ` `// as power of two ` ` ` `if` `(isPowerOfTwo(a[i])) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// count total number ` ` ` `// of unordered pairs ` ` ` `int` `ans = (count * (count - 1)) / 2; ` ` ` ` ` `Console.WriteLine( ans); ` `} ` ` ` `// Driver code ` ` ` ` ` `static` `public` `void` `Main (){ ` ` ` `int` `[]a = { 2, 5, 8, 16, 128 }; ` ` ` ` ` `int` `n = a.Length; ` ` ` `Count_pairs(a, n); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed ` `// by Sach_Code ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to Count unordered ` `// pairs (i, j) in array such that ` `// product of a[i] and a[j] can be ` `// expressed as power of two ` ` ` `/* Function to check if x is power of 2*/` `function` `isPowerOfTwo(` `$x` `) ` `{ ` ` ` `/* First x in the below expression is ` ` ` `for the case when x is 0 */` ` ` `return` `(` `$x` `&& (!(` `$x` `& (` `$x` `- 1)))); ` `} ` ` ` `// Function to Count unordered pairs ` `function` `Count_pairs(` `$a` `, ` `$n` `) ` `{ ` ` ` `$count` `= 0; ` ` ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// is a number can be expressed ` ` ` `// as power of two ` ` ` `if` `(isPowerOfTwo(` `$a` `[` `$i` `])) ` ` ` `$count` `++; ` ` ` `} ` ` ` ` ` `// count total number ` ` ` `// of unordered pairs ` ` ` `$ans` `= (` `$count` `* (` `$count` `- 1)) / 2; ` ` ` ` ` `echo` `$ans` `, ` `"\n"` `; ` `} ` ` ` `// Driver code ` `$a` `= ` `array` `( 2, 5, 8, 16, 128 ); ` ` ` `$n` `= sizeof(` `$a` `); ` ` ` `Count_pairs(` `$a` `, ` `$n` `); ` ` ` `// This code is contributed ` `// by Sach_code ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity: **O(N), where N is the number of elements in the array.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count pairs in Array whose product is a Kth power of any positive integer
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Count of unordered pairs (x, y) of Array which satisfy given equation
- Queries to count the number of unordered co-prime pairs from 1 to N
- Count of pairs whose bitwise AND is a power of 2
- Count of even and odd power pairs in an Array
- Count pairs in array such that one element is power of another
- Count ordered pairs with product less than N
- Count number of ordered pairs with Even and Odd Product
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count all distinct pairs with product equal to K
- Count pairs in a sorted array whose product is less than k
- XOR of pairwise sum of every unordered pairs in an array
- Count of pairs in an array whose product is a perfect square
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Count of index pairs in array whose range product is a positive integer
- Find the sum of power of bit count raised to the power B
- Number of pairs whose sum is a power of 2
- Pairs such that one is a power multiple of other

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.