Given an array of size N, find count of subsequences which when multiplied result in a number which is a power of 2.

Examples:

Input : A[] = {1, 2, 3} Output : 3 Explanation: There are 3 such subsequences {1}, {2} and {1, 2}. Input : A[] = {3, 5, 9} Output : 0 Explanation: There is no such subsequence.

From the properties of power of two, we can see that it can be expressed only as a product of numbers which itself is power of 2. So first we traverse the array and count the total of numbers in the array which are power of two. Let’s say there are N such numbers in the array. We can either choose 1 or 2 or 3 or … or N such numbers to get a subsequence which is when multiplied results in a number which is power of two.

Therefore the required answer will be:

**answer = + + … + **

**answer=
**

Below is the implementation of above idea.

## C++

`// CPP program to count number of subsequences ` `// which when multiplied result in a power of 2. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if num is power of ` `// two or not. ` `bool` `isPowerOf2(` `int` `num) ` `{ ` ` ` `if` `(num == 0) ` ` ` `return` `false` `; ` ` ` ` ` `if` `(num == 1) ` ` ` `return` `true` `; ` ` ` ` ` `if` `(num & (num - 1)) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// counting all subsequences whose product ` `// is power of 2. ` `int` `countSubsequence(` `int` `a[], ` `int` `size) ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < size; i++) ` ` ` `if` `(isPowerOf2(a[i])) ` ` ` `count++; ` ` ` `return` `(` `int` `)(` `pow` `(2, count)) - 1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 2, 3 }; ` ` ` `cout << countSubsequence(a, 3) << endl; ` ` ` `int` `b[] = { 3, 5, 9 }; ` ` ` `cout << countSubsequence(b, 3) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA program to count number of ` `// subsequences which when multiplied ` `// result in a power of 2. ` `import` `java.io.*; ` `import` `java.math.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to check if num is ` ` ` `// power of two or not. ` ` ` `static` `boolean` `isPowerOf2(` `int` `num) ` ` ` `{ ` ` ` `if` `(num == ` `0` `) ` ` ` `return` `false` `; ` ` ` ` ` `if` `(num == ` `1` `) ` ` ` `return` `true` `; ` ` ` ` ` `if` `(num / ` `2` `== (num - ` `1` `) / ` `2` `) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// counting all subsequences whose ` ` ` `// product is power of 2. ` ` ` `static` `int` `countSubsequence(` `int` `a[], ` ` ` `int` `size) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < size; i++) ` ` ` `if` `(isPowerOf2(a[i])) ` ` ` `count++; ` ` ` `return` `(` `int` `)(Math.pow(` `2` `, count)) - ` `1` `; ` ` ` `} ` ` ` ` ` `// Driver ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `a[] = { ` `1` `, ` `2` `, ` `3` `}; ` ` ` `System.out.println(countSubsequence(a, ` `3` `)); ` ` ` `int` `b[] = { ` `3` `, ` `5` `, ` `9` `}; ` ` ` `System.out.println(countSubsequence(b, ` `3` `)) ; ` ` ` `} ` `} ` ` ` `/*This code is contributed by Nikita Tiwari.*/` |

*chevron_right*

*filter_none*

## Python

`# Python program to count number of ` `# subsequences which when multiplied ` `# result in a power of 2. ` ` ` `# Function to check if num is power ` `# of two or not. ` `def` `isPowerOf2(num) : ` ` ` `if` `(num ` `=` `=` `0` `) : ` ` ` `return` `False` ` ` ` ` `if` `(num ` `=` `=` `1` `) : ` ` ` `return` `True` ` ` ` ` `if` `(num & (num ` `-` `1` `)) : ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` `# counting all subsequences whose ` `# product is power of 2. ` `def` `countSubsequence(a, size) : ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(` `0` `,size) : ` ` ` `if` `(isPowerOf2(a[i])) : ` ` ` `count ` `=` `count ` `+` `1` ` ` `return` `(` `int` `)(` `pow` `(` `2` `, count)) ` `-` `1` ` ` `# Driver code ` `a ` `=` `[ ` `1` `, ` `2` `, ` `3` `]; ` `print` `countSubsequence(a, ` `3` `) ` `b ` `=` `[ ` `3` `, ` `5` `, ` `9` `] ` `print` `countSubsequence(b, ` `3` `) ` ` ` `# This code is contributed by Nikita Tiwari ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number of ` `// subsequences which when multiplied ` `// result in a power of 2. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to check if num is ` ` ` `// power of two or not. ` ` ` `static` `bool` `isPowerOf2(` `int` `num) ` ` ` `{ ` ` ` `if` `(num == 0) ` ` ` `return` `false` `; ` ` ` ` ` `if` `(num == 1) ` ` ` `return` `true` `; ` ` ` ` ` `if` `(num / 2 == (num - 1) / 2) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// counting all subsequences whose ` ` ` `// product is power of 2. ` ` ` `static` `int` `countSubsequence(` `int` `[]a, ` ` ` `int` `size) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < size; i++) ` ` ` `if` `(isPowerOf2(a[i])) ` ` ` `count++; ` ` ` `return` `(` `int` `)(Math.Pow(2, count)) - 1; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]a = { 1, 2, 3 }; ` ` ` `Console.WriteLine(countSubsequence(a, 3)); ` ` ` `int` `[]b = { 3, 5, 9 }; ` ` ` `Console.WriteLine(countSubsequence(b, 3)) ; ` ` ` `} ` `} ` ` ` `/*This code is contributed by vt_m.*/` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count number ` `// of subsequences which when ` `// multiplied result in a power ` `// of 2. ` ` ` `// Function to check if num ` `// is power of two or not. ` `function` `isPowerOf2( ` `$num` `) ` `{ ` ` ` `if` `(` `$num` `== 0) ` ` ` `return` `false; ` ` ` ` ` `if` `(` `$num` `== 1) ` ` ` `return` `true; ` ` ` ` ` `if` `(` `$num` `& (` `$num` `- 1)) ` ` ` `return` `false; ` ` ` ` ` `return` `true; ` `} ` ` ` `// counting all subsequences whose ` `// product is power of 2. ` `function` `countSubsequence( ` `$a` `, ` `$size` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$size` `; ` `$i` `++) ` ` ` `if` `(isPowerOf2(` `$a` `[` `$i` `])) ` ` ` `$count` `++; ` ` ` `return` `pow(2, ` `$count` `) - 1; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$a` `= ` `array` `(1, 2, 3); ` ` ` `echo` `countSubsequence(` `$a` `, 3) ,` `"\n"` `; ` ` ` `$b` `= ` `array` `(3, 5, 9); ` ` ` `echo` `countSubsequence(` `$b` `, 3) ; ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

3 0

This article is contributed by **ShivamKD**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.

## Recommended Posts:

- Represent n as the sum of exactly k powers of two | Set 2
- Number of ways to partition a string into two balanced subsequences
- Find all combinations of two equal sum subsequences
- Find two equal subsequences of maximum length with at least one different index
- Count of subsequences whose product is a difference of square of two integers
- Form a number using corner digits of powers
- Find all powers of 2 less than or equal to a given number
- Minimum number of distinct powers of 2 required to express a given binary number
- Count subsequences with same values of Bitwise AND, OR and XOR
- Sum of width (max and min diff) of all Subsequences
- Product of all Subsequences of size K except the minimum and maximum Elements
- Number of Subsequences with Even and Odd Sum
- Number of Subsequences with Even and Odd Sum | Set 2
- Count the number of subsequences of length k having equal LCM and HCF
- Count the number of contiguous increasing and decreasing subsequences in a sequence
- Count of possible subarrays and subsequences using given length of Array
- Count subsequences which contains both the maximum and minimum array element
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Count Distinct Subsequences
- Count all increasing subsequences