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.
Input: N = 63
Output: 32 16 8 4 2 1
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
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:
32 16 8 4 2 1
Time Complexity: O(log N)
Auxiliary Space: O(1)
- Distinct powers of a number N such that the sum is equal to K
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Minimum number of distinct powers of 2 required to express a given binary number
- Find k numbers which are powers of 2 and have sum N | Set 1
- Find a Number X whose sum with its digits is equal to N
- Find a number x such that sum of x and its digits is equal to given n.
- Minimum number of given powers of 2 required to represent a number
- Find the sum of numbers from 1 to n excluding those which are powers of K
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Find a number K such that Array contains at least K numbers greater than or equal to K
- Find Largest Special Prime which is less than or equal to a given number
- Representation of a number in powers of other
- Finding powers of any number P in N!
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Check if a number can be represented as sum of non zero powers of 2
- Find the number of operations required to make all array elements Equal
- Form a number using corner digits of powers
- Number of triangles possible with given lengths of sticks which are powers of 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.