Reach N from 1 by incrementing by 1 or doubling the value at most D times
Last Updated :
11 Feb, 2022
Given an integer N and an integer D, the task is to reach N from 1 in minimum moves by either adding 1 or doubling the value, but the doubling can be done at most D times.
Examples:
Input: N = 20, D = 4
Output: 5
Explanation: The flow can be seen as 1 -> 2 -> 4 -> 5 -> 10 -> 20
Input: N = 10, D = 0
Output: 9
Approach: The task can be solved using recursion:
- Declare a variable to store the minimum moves as answer
- First check if the target is reached, if yes find store the minimum of current moves and answer in ans
- Then check if the doubling moves D has been exhausted, but target has not been reached yet,
- Then add the remaining moves as incrementing moves by adding (N-current_value) in current_moves.
- Find the minimum of current_moves and ans, and store in ans
- If the current_value has crossed N, return
- If none of the above cases match, recursively call the function to do both of the below one by one:
- double the current_value
- add 1 to current_value.
- Return the final ans.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
void move( int & N, int & D, long s,
int cd, int temp)
{
if (s == N) {
ans = min(ans, temp);
return ;
}
if (cd == D && s <= N) {
temp += (N - s);
ans = min(ans, temp);
return ;
}
if (s > N)
return ;
move(N, D, s * 2, cd + 1, temp + 1);
move(N, D, s + 1, cd, temp + 1);
}
int minMoves( int N, int D)
{
ans = N;
move(N, D, 1, 0, 0);
return ans;
}
int main()
{
int N = 20, D = 4;
cout << minMoves(N, D);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static int ans = 0 ;
static void move( int N, int D, long s, int cd, int temp)
{
if (s == N) {
ans = Math.min(ans, temp);
return ;
}
if (cd == D && s <= N) {
temp += (N - s);
ans = Math.min(ans, temp);
return ;
}
if (s > N)
return ;
move(N, D, s * 2 , cd + 1 , temp + 1 );
move(N, D, s + 1 , cd, temp + 1 );
}
static int minMoves( int N, int D)
{
ans = N;
move(N, D, 1 , 0 , 0 );
return ans;
}
public static void main (String[] args) {
int N = 20 , D = 4 ;
System.out.print(minMoves(N, D));
}
}
|
Python3
ans = 0 ;
def move(N, D, s, cd, temp):
global ans;
if (s = = N):
ans = min (ans, temp);
return ;
if (cd = = D and s < = N):
temp + = (N - s);
ans = min (ans, temp);
return ;
if (s > N):
return ;
move(N, D, s * 2 , cd + 1 , temp + 1 );
move(N, D, s + 1 , cd, temp + 1 );
def minMoves(N, D):
global ans;
ans = N;
move(N, D, 1 , 0 , 0 );
return ans;
if __name__ = = '__main__' :
N = 20 ;
D = 4 ;
print (minMoves(N, D));
|
C#
using System;
class GFG {
static int ans = 0;
static void move( int N, int D, long s, int cd, int temp)
{
if (s == N) {
ans = Math.Min(ans, temp);
return ;
}
if (cd == D && s <= N) {
temp += ( int )(N - s);
ans = Math.Min(ans, temp);
return ;
}
if (s > N)
return ;
move(N, D, s * 2, cd + 1, temp + 1);
move(N, D, s + 1, cd, temp + 1);
}
static int minMoves( int N, int D)
{
ans = N;
move(N, D, 1, 0, 0);
return ans;
}
public static void Main () {
int N = 20, D = 4;
Console.Write(minMoves(N, D));
}
}
|
Javascript
<script>
let ans = 0;
function move(N, D, s,
cd, temp) {
if (s == N) {
ans = Math.min(ans, temp);
return ;
}
if (cd == D && s <= N) {
temp += (N - s);
ans = Math.min(ans, temp);
return ;
}
if (s > N)
return ;
move(N, D, s * 2, cd + 1, temp + 1);
move(N, D, s + 1, cd, temp + 1);
}
function minMoves(N, D) {
ans = N;
move(N, D, 1, 0, 0);
return ans;
}
let N = 20, D = 4;
document.write(minMoves(N, D));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...