Minimum steps to reach the Nth stair in jumps of perfect power of 2
Last Updated :
11 Jul, 2022
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: 4
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++
#include "bits/stdc++.h"
using namespace std;
int stepRequired( int N)
{
int cnt = 0;
while (N) {
N = N & (N - 1);
cnt++;
}
return cnt;
}
int main()
{
int N = 23;
cout << stepRequired(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int stepRequired( int N)
{
int cnt = 0 ;
while (N > 0 ) {
N = N & (N - 1 );
cnt++;
}
return cnt;
}
public static void main(String[] args)
{
int N = 23 ;
System.out.print(stepRequired(N));
}
}
|
Python3
def stepRequired(N):
cnt = 0 ;
while (N > 0 ):
N = N & (N - 1 );
cnt + = 1 ;
return cnt;
if __name__ = = '__main__' :
N = 23 ;
print (stepRequired(N));
|
C#
using System;
class GFG{
static int stepRequired( int N)
{
int cnt = 0;
while (N > 0)
{
N = N & (N - 1);
cnt++;
}
return cnt;
}
public static void Main(String[] args)
{
int N = 23;
Console.Write(stepRequired(N));
}
}
|
Javascript
<script>
function stepRequired(N)
{
let cnt = 0;
while (N)
{
N = N & (N - 1);
cnt++;
}
return cnt;
}
let N = 23;
document.write(stepRequired(N));
</script>
|
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;
while (N>0)
{
N-= pow (2,( int )log2(N));
cnt++;
}
return cnt;
}
int main()
{
int N = 23;
cout << stepRequired(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int stepRequired( int N)
{
int cnt = 0 ;
while (N> 0 )
{
N-= Math.pow( 2 , ( int )(Math.log(N) / Math.log( 2 )));
cnt++;
}
return cnt;
}
public static void main(String[] args) {
int N = 23 ;
System.out.println(stepRequired(N));
}
}
|
Python3
import math
def stepRequired(N):
cnt = 0
while (N > 0 ):
N - = math. pow ( 2 ,math.floor(math.log2(N)))
cnt + = 1
return cnt
N = 23
print (stepRequired(N))
|
C#
using System;
class GFG{
static int stepRequired( int N)
{
int cnt = 0;
while (N > 0)
{
N-=( int )Math.Pow(2,( int )(Math.Log(N,2)));
cnt++;
}
return cnt;
}
public static void Main(String[] args)
{
int N = 23;
Console.Write(stepRequired(N));
}
}
|
Javascript
<script>
function stepRequired(N)
{
let cnt = 0;
while (N > 0)
{
N -= Math.pow(2,Math.floor(Math.log2(N)));
cnt++;
}
return cnt;
}
let N = 23;
document.write(stepRequired(N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...