# Reach N from 1 by incrementing by 1 or doubling the value at most D times

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
• Return the final ans.

Below is the implementation of the above approach.

## C++

 `// C++ code to implement the approach` `#include ` `using` `namespace` `std;`   `int` `ans = 0;`   `// Utility function to find ` `// the minimum number of moves required` `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);` `}`   `// Function to call the utility function` `int` `minMoves(``int` `N, ``int` `D)` `{` `    ``ans = N;` `    ``move(N, D, 1, 0, 0);` `    ``return` `ans;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `N = 20, D = 4;`   `    ``cout << minMoves(N, D);` `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;`   `class` `GFG {`   `  ``static` `int` `ans = ``0``;`   `  ``// Utility function to find ` `  ``// the minimum number of moves required` `  ``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``);` `  ``}`   `  ``// Function to call the utility function` `  ``static` `int` `minMoves(``int` `N, ``int` `D)` `  ``{` `    ``ans = N;` `    ``move(N, D, ``1``, ``0``, ``0``);` `    ``return` `ans;` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `main (String[] args) {` `    ``int` `N = ``20``, D = ``4``;`   `    ``System.out.print(minMoves(N, D));` `  ``}` `}`   `// This code is contributed by hrithikgarg03188.`

## Python3

 `# Python program for the above approach` `ans ``=` `0``;`   `# Utility function to find` `# the minimum number of moves required` `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``);`   `# Function to call the utility function` `def` `minMoves(N, D):` `    ``global` `ans;` `    ``ans ``=` `N;` `    ``move(N, D, ``1``, ``0``, ``0``);` `    ``return` `ans;`   `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``N ``=` `20``;` `    ``D ``=` `4``;`   `    ``print``(minMoves(N, D));`   `# This code is contributed by 29AjayKumar`

## C#

 `// C# program for the above approach` `using` `System;` `class` `GFG {`   `  ``static` `int` `ans = 0;`   `  ``// Utility function to find ` `  ``// the minimum number of moves required` `  ``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);` `  ``}`   `  ``// Function to call the utility function` `  ``static` `int` `minMoves(``int` `N, ``int` `D)` `  ``{` `    ``ans = N;` `    ``move(N, D, 1, 0, 0);` `    ``return` `ans;` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `Main () {` `    ``int` `N = 20, D = 4;`   `    ``Console.Write(minMoves(N, D));` `  ``}` `}`   `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`5`

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

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next