Given **N** stairs, the task is to find the minimum number of jumps of perfect power of **2** requires to reach the **Nth** stair.

**Examples:**

Input:N = 5

Output:

Explanation:

We can take jumps from0->1->4.

So the minimum jumps require are 2.

Input:N = 23

Output:4

Explanation:

We can take jumps from0->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 **2 ^{i}** for all set bit

**index i**is equals to

**N**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include "bits/stdc++.h" ` `using` `namespace` `std; ` ` ` `// Function to count the number of jumps ` `// required to reach Nth stairs. ` `int` `stepRequired(` `int` `N) ` `{ ` ` ` ` ` `int` `cnt = 0; ` ` ` ` ` `// Till N becomes 0 ` ` ` `while` `(N) { ` ` ` ` ` `// Removes the set bits from ` ` ` `// the right to left ` ` ` `N = N & (N - 1); ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// Number of stairs ` ` ` `int` `N = 23; ` ` ` ` ` `// Function Call ` ` ` `cout << stepRequired(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` ` ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to count the number of jumps ` `// required to reach Nth stairs. ` `static` `int` `stepRequired(` `int` `N) ` `{ ` ` ` ` ` `int` `cnt = ` `0` `; ` ` ` ` ` `// Till N becomes 0 ` ` ` `while` `(N > ` `0` `) { ` ` ` ` ` `// Removes the set bits from ` ` ` `// the right to left ` ` ` `N = N & (N - ` `1` `); ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Number of stairs ` ` ` `int` `N = ` `23` `; ` ` ` ` ` `// Function Call ` ` ` `System.out.print(stepRequired(N)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to count the number of jumps ` `# required to reach Nth stairs. ` `def` `stepRequired(N): ` ` ` ` ` `cnt ` `=` `0` `; ` ` ` ` ` `# Till N becomes 0 ` ` ` `while` `(N > ` `0` `): ` ` ` ` ` `# Removes the set bits from ` ` ` `# the right to left ` ` ` `N ` `=` `N & (N ` `-` `1` `); ` ` ` `cnt ` `+` `=` `1` `; ` ` ` `return` `cnt; ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Number of stairs ` ` ` `N ` `=` `23` `; ` ` ` ` ` `# Function Call ` ` ` `print` `(stepRequired(N)); ` ` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to count the number of ` `// jumps required to reach Nth stairs. ` `static` `int` `stepRequired(` `int` `N) ` `{ ` ` ` `int` `cnt = 0; ` ` ` ` ` `// Till N becomes 0 ` ` ` `while` `(N > 0) ` ` ` `{ ` ` ` ` ` `// Removes the set bits from ` ` ` `// the right to left ` ` ` `N = N & (N - 1); ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Number of stairs ` ` ` `int` `N = 23; ` ` ` ` ` `// Function Call ` ` ` `Console.Write(stepRequired(N)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

4

**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.

## Recommended Posts:

- 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 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.