- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Powers of two and subsequences in C++

In this problem, we are given an array of N integers. Our task is to find the count of subsequences that can be formed such that if their elements are multiplied, they result in a number which is a power of two.

Let’s take an example to understand the problem,

**Input** − arr = [2, 5, 4]

**Output** − 3

**Explanation** − subsequences [2], [4] and [2, 4] give the desired result.

To solve this problem, we need to understand the logic of power.

Only those numbers with are powers of 2 will multiply to give the desired result. So, we have to consider only those subsequences from the array that itself are some powers of 2.

So, if in the array there are M elements that are powers of 2, then the count of sub-arrays will be **2 ^{M} - 1**

## Example

The program to show the implementation of our solution

#include <iostream> #include <math.h> using namespace std; bool isPowerTwo(int num) { if (num == 0) return false; if (num == 1) return true; if (num & (num - 1)) return false; return true; } int SubsequenceWithPowerTwo(int arr[], int N) { int count = 0; for (int i = 0; i < N; i++) if (isPowerTwo(arr[i])) count++; return (int)(pow(2, count)) - 1; } int main() { int arr[] = {5, 4, 8, 12, 32, 9 }; int N = sizeof(arr)/sizeof(arr[0]); cout<<"No. of subsequences which multiply to a number which is a power of 2 are : "; cout<<SubsequenceWithPowerTwo(arr, N)<<endl; return 0; }

## Output

No. of subsequences which multiply to a number which is a power of 2 are : 7

- Related Questions & Answers
- Print all integers that are sum of powers of two given numbers in C++
- Number of Matching Subsequences in C++
- Austin Powers in Python
- C++ Representation of a Number in Powers of Other
- Print all prime factors and their powers in C++
- Powers of 2 to required sum in C++
- Increasing Subsequences in C++
- Distinct Subsequences in C++
- Print all subsequences of a string in C++
- Find subsequences with maximum Bitwise AND and Bitwise OR in Python
- Distinct Subsequences II in C++
- Distinct Subsequences in C++ Programming
- Find k numbers which are powers of 2 and have sum N in C++
- Product of all Subsequences of size K except the minimum and maximum Elements in C++
- Count of subsequences having maximum distinct elements in C++