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 -> 20Input: 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++
// C++ code to implement the approach #include <bits/stdc++.h> 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
<script> // JavaScript code for the above approach let ans = 0; // Utility function to find // the minimum number of moves required 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 to call the utility function function minMoves(N, D) { ans = N; move(N, D, 1, 0, 0); return ans; } // Driver code let N = 20, D = 4; document.write(minMoves(N, D)); // This code is contributed by Potta Lokesh </script> |
Output
5
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...