Open In App

Minimum steps to reach the Nth stair in jumps of perfect power of 2

Last Updated : 11 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 from 0->4->5.
So the minimum jumps require are 2.

Input: N = 23 
Output:
Explanation: 
We can take jumps from 0->1->3->7->23
So the minimum jumps required are 4. 
 

First 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:  

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;
}


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


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


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


Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to count the number of jumps
// required to reach Nth stairs.
function stepRequired(N)
{
    let 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
 
// Number of stairs
let N = 23;
 
// Function Call
document.write(stepRequired(N));
 
// This code is contributed by Surbhi Tyagi
 
</script>


Output: 

4

 

Time Complexity: O(log N)
 Auxiliary Space: O(1)

Second Approach: Since the jumps are required to be in perfect power of 2. We can observe that log2 function gives the highest perfect power of 2 which can be achieved less than N if we typecast it to an integer. So we can subtract the pow(2,(int)log2(N)) each time from N till its value is greater than 0 while incrementing cnt at the same time.

C++14




#include <bits/stdc++.h>
 
using namespace std;
 
int stepRequired(int& N)
{
  
    int cnt = 0;
     
    //until N is reached
    while(N>0)
    {
        //subtract highest perfect power of 2 we can reach from previous level
        N-=pow(2,(int)log2(N));
         
        //increment cnt for total number of steps taken
        cnt++;
    }
    return cnt;
}
 
int main()
{
  
    // Number of stairs
    int N = 23;
  
    // Function Call
    cout << stepRequired(N);
    return 0;
}


Java




// Java program for above approach
import java.util.*;
 
class GFG
{
 
static int stepRequired(int N)
{
   
    int cnt = 0;
      
    //until N is reached
    while(N>0)
    {
        //subtract highest perfect power of 2 we can reach from previous level
        N-= Math.pow(2, (int)(Math.log(N) / Math.log(2)));
          
        //increment cnt for total number of steps taken
        cnt++;
    }
    return cnt;
}
 
public static void main(String[] args) {
         
    // Number of stairs
    int N = 23;
   
    // Function Call
    System.out.println(stepRequired(N));
}
}
 
// This code is contributed by sanjoy_62.


Python3




# Python code is contributed by shinjanpatra
import math
 
def stepRequired(N):
  
    cnt = 0
     
    # until N is reached
    while(N > 0):
 
        # subtract highest perfect power of 2 we can reach from previous level
        N -= math.pow(2,math.floor(math.log2(N)))
         
        # increment cnt for total number of steps taken
        cnt += 1
 
    return cnt
 
# driver code
  
# Number of stairs
N = 23
  
# Function Call
print(stepRequired(N))
 
# This code is contributed by shinjanpatra


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;
  
    //until N is reached
    while (N > 0)
    {
        //subtract highest perfect power of 2 we can reach from previous level
        N-=(int)Math.Pow(2,(int)(Math.Log(N,2)));
  
          
        //increment cnt for total number of steps taken
        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 aditya942003patil


Javascript




<script>
 
// JavaScript code is contributed by shinjanpatra
function stepRequired(N)
{
  
    let cnt = 0;
     
    // until N is reached
    while(N > 0)
    {
        // subtract highest perfect power of 2 we can reach from previous level
        N -= Math.pow(2,Math.floor(Math.log2(N)));
         
        // increment cnt for total number of steps taken
        cnt++;
    }
    return cnt;
}
 
// driver code
  
// Number of stairs
let N = 23;
  
// Function Call
document.write(stepRequired(N));
 
// This code is contributed by shinjanpatra
 
</script>


Time Complexity: O(log N) 

Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads