Given a positive number **N**, the task is to find out all the perfect powers of two which are less than or equal to the given number **N**.

**Examples: **

Input:N = 63

Output:32 16 8 4 2 1

Explanation:

There are total of 6 powers of 2, which are less than or equal to the given number N.

Input:N = 193

Output:128 64 32 16 8 4 2 1

Explaination:

There are total of 8 powers of 2, which are less than or equal to the given number N.

**Naive Approach:** The idea is to traverse each number from N to 1 and check if it is a perfect power of 2 or not. If yes, then print that number.

**Another Approach:** The idea is to find all powers of 2 and simply print the powers that are lesser than or equal to N.

**Another Approach:** The idea is based on the concept that all powers of 2 has all bits set, in its binary form. Bitset function is used in this approach solve the above problem. Below are the steps:

- Find the largest power of 2(say
**temp**) which is used to evaluate the number less than or equal to**N**. - Initialise an bitset array
**arr[]**of maximum size**64**, to store the binary representation of the given number**N**. - Reset all the bits in the bitset array using reset() function.
- Iterate a loop from
**total to 0**, and sequentially make each bit**1**, and find the value of that binary expression and then reset the bit.

Below is the implementation of the above approach:

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `const` `int` `MAX = 64; ` ` ` `// Function to return max exponent of ` `// 2 which evaluates a number less ` `// than or equal to N ` `int` `max_exponent(` `int` `n) ` `{ ` ` ` `return` `(` `int` `)(log2(n)); ` `} ` ` ` `// Function to print all the powers ` `// of 2 less than or equal to N ` `void` `all_powers(` `int` `N) ` `{ ` ` ` `bitset<64> arr(N); ` ` ` ` ` `// Reset all the bits ` ` ` `arr.reset(); ` ` ` ` ` `int` `total = max_exponent(N); ` ` ` ` ` `// Iterate from total to 0 ` ` ` `for` `(` `int` `i = total; i >= 0; i--) { ` ` ` ` ` `// Reset the next bit ` ` ` `arr.reset(i + 1); ` ` ` ` ` `// Set the current bit ` ` ` `arr.set(i); ` ` ` ` ` `// Value of the binary expression ` ` ` `cout << arr.to_ulong() << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given Number ` ` ` `int` `N = 63; ` ` ` ` ` `// Function Call ` ` ` `all_powers(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

32 16 8 4 2 1

**Time Complexity:** *O(log N)*

**Auxiliary Space:** *O(1)*

## Recommended Posts:

- Find Largest Special Prime which is less than or equal to a given number
- Highest and Smallest power of K less than and greater than equal to N respectively
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find all factorial numbers less than or equal to n
- Find all possible subarrays having product less than or equal to K
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Sum of all array elements less than X and greater than Y for Q queries
- Highest power of 2 less than or equal to given number
- Print a number strictly less than a given number such that all its digits are distinct.
- Number of elements less than or equal to a number in a subarray : MO's Algorithm
- Minimum swaps required to bring all elements less than or equal to k together
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Print all Semi-Prime Numbers less than or equal to N
- Print all Strong numbers less than or equal to N
- Print all proper fractions with denominators less than equal to N
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Largest number less than N with digit sum greater than the digit sum of N
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Find maximum sum array of length less than or equal to m
- Find unique pairs such that each element is less than or equal to N

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.