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.