Given N stairs, the task is to find the minimum number of jumps of perfect power of 2 requires to reach the Nth stair.
Input: N = 5
We can take jumps from 0->1->4.
So the minimum jumps require are 2.
Input: N = 23
We can take jumps from 0->1->2->4->16.
So the minimum jumps required are 4.
Approach: Since the jumps are required to be in perfect power of 2. So the count of set bit in the given number N is the minimum number of jumps required to reach Nth stair as the summation of 2i for all set bit index i is equals to N.
Below is the implementation of the above approach:
Time Complexity: O(log N)
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.
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Minimum number of jumps to reach end
- Count minimum factor jumps required to reach the end of an Array
- Find the minimum number of steps to reach M from N
- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Count the minimum steps to reach 0 from the given integer N
- Minimum steps for increasing and decreasing Array to reach either 0 or N
- Climb n-th stair with all jumps from 1 to n allowed (Three Different Approaches)
- Find the number of stair steps
- Count ways to reach the n'th stair
- Find the number of ways to reach Kth step in stair case
- Reach the numbers by making jumps of two given lengths
- Check if it is possible to reach a number by making jumps of two given length
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Perfect power (1, 4, 8, 9, 16, 25, 27, ...)
- Find the number of jumps to reach X in the number line from zero
- Count perfect power of K in a range [L, R]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.