Convert N to M with given operations using dynamic programming Last Updated : 21 Aug, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two integers N and M and the task is to convert N to M with the following operations: Multiply N by 2 i.e. N = N * 2.Subtract 1 from N i.e. N = N - 1. Examples: Input: N = 4, M = 6 Output: 2 Perform operation 2: N = N - 1 = 4 - 1 = 3 Perform operation 1: N = N * 2 = 3 * 2 = 6 Input: N = 10, M = 1 Output: 9 Approach: Create an array dp[] of size MAX = 105 + 5 to store the answer in order to prevent the same computation again and again and initialize all the array elements with -1. If N ? 0 or N ? MAX means it can not be converted to M so return MAX.If N = M then return 0 as N got converted to M.Else find the value at dp[N] if it is not -1, it means it has been calculated earlier so return dp[N].If it is -1 then will call the recursive function as 2 * N and N - 1 and return the minimum because if N is odd then it can be reached only by performing N - 1 operation and if N is even then 2 * N operations have to be performed so check both the possibilities and return the minimum. Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, m; int dp[N]; // Function to return the minimum // number of given operations // required to convert n to m int minOperations(int k) { // If k is either 0 or out of range // then return max if (k <= 0 || k >= 2e4) { return 1e9; } // If k = m then conversion is // complete so return 0 if (k == m) { return 0; } int& ans = dp[k]; // If it has been calculated earlier if (ans != -1) { return ans; } ans = 1e9; // Call for 2*k and k-1 and return // the minimum of them. If k is even // then it can be reached by 2*k operations // and If k is odd then it can be reached // by k-1 operations so try both cases // and return the minimum of them ans = 1 + min(minOperations(2 * k), minOperations(k - 1)); return ans; } // Driver code int main() { n = 4, m = 6; memset(dp, -1, sizeof(dp)); cout << minOperations(n); return 0; } Java // Java implementation of the approach import java.util.*; class GFG { static final int N = 10000; static int n, m; static int[] dp = new int[N]; // Function to return the minimum // number of given operations // required to convert n to m static int minOperations(int k) { // If k is either 0 or out of range // then return max if (k <= 0 || k >= 10000) return 1000000000; // If k = m then conversion is // complete so return 0 if (k == m) return 0; dp[k] = dp[k]; // If it has been calculated earlier if (dp[k] != -1) return dp[k]; dp[k] = 1000000000; // Call for 2*k and k-1 and return // the minimum of them. If k is even // then it can be reached by 2*k operations // and If k is odd then it can be reached // by k-1 operations so try both cases // and return the minimum of them dp[k] = 1 + Math.min(minOperations(2 * k), minOperations(k - 1)); return dp[k]; } // Driver Code public static void main(String[] args) { n = 4; m = 6; Arrays.fill(dp, -1); System.out.println(minOperations(n)); } } // This code is contributed by // sanjeev2552 Python3 # Python3 implementation of the approach N = 1000 dp = [-1] * N # Function to return the minimum # number of given operations # required to convert n to m def minOperations(k): # If k is either 0 or out of range # then return max if (k <= 0 or k >= 1000): return 1e9 # If k = m then conversion is # complete so return 0 if (k == m): return 0 dp[k] = dp[k] # If it has been calculated earlier if (dp[k] != -1): return dp[k] dp[k] = 1e9 # Call for 2*k and k-1 and return # the minimum of them. If k is even # then it can be reached by 2*k operations # and If k is odd then it can be reached # by k-1 operations so try both cases # and return the minimum of them dp[k] = 1 + min(minOperations(2 * k), minOperations(k - 1)) return dp[k] # Driver code if __name__ == '__main__': n = 4 m = 6 print(minOperations(n)) # This code is contributed by ashutosh450 C# // C# implementation of the approach using System; using System.Linq; class GFG { static int N = 10000; static int n, m; static int[] dp = Enumerable.Repeat(-1, N).ToArray(); // Function to return the minimum // number of given operations // required to convert n to m static int minOperations(int k) { // If k is either 0 or out of range // then return max if (k <= 0 || k >= 10000) return 1000000000; // If k = m then conversion is // complete so return 0 if (k == m) return 0; dp[k] = dp[k]; // If it has been calculated earlier if (dp[k] != -1) return dp[k]; dp[k] = 1000000000; // Call for 2*k and k-1 and return // the minimum of them. If k is even // then it can be reached by 2*k operations // and If k is odd then it can be reached // by k-1 operations so try both cases // and return the minimum of them dp[k] = 1 + Math.Min(minOperations(2 * k), minOperations(k - 1)); return dp[k]; } // Driver Code public static void Main(String[] args) { n = 4; m = 6; //Arrays.fill(dp, -1); Console.Write(minOperations(n)); } } // This code is contributed by // Mohit kumar 29 JavaScript <script> let N = 10000; let n, m; let dp = new Array(N); function minOperations(k) { // If k is either 0 or out of range // then return max if (k <= 0 || k >= 10000) return 1000000000; // If k = m then conversion is // complete so return 0 if (k == m) return 0; dp[k] = dp[k]; // If it has been calculated earlier if (dp[k] != -1) return dp[k]; dp[k] = 1000000000; // Call for 2*k and k-1 and return // the minimum of them. If k is even // then it can be reached by 2*k operations // and If k is odd then it can be reached // by k-1 operations so try both cases // and return the minimum of them dp[k] = 1 + Math.min(minOperations(2 * k), minOperations(k - 1)); return dp[k]; } // Driver Code n = 4; m = 6; for(let i = 0; i < dp.length; i++) { dp[i] = -1; } document.write(minOperations(n)); // This code is contributed by unknown2108 </script> Output: 2 Comment More infoAdvertise with us Next Article Ackermann's function using Dynamic programming M md1844 Follow Improve Article Tags : Dynamic Programming Mathematical DSA Numbers Practice Tags : Dynamic ProgrammingMathematicalNumbers Similar Reads Ackermann's function using Dynamic programming Given two non-zero integers M and N, the problem is to compute the result of the Ackermann function based on some particular equations. Ackermann function is defined as: Examples: Input: M = 2, N = 2Output: 7 Input: M = 2, N = 7Output: 6141004759 The approach for Ackermann function described in this 13 min read Ackermann's function using Dynamic programming Given two non-zero integers M and N, the problem is to compute the result of the Ackermann function based on some particular equations. Ackermann function is defined as: Examples: Input: M = 2, N = 2Output: 7 Input: M = 2, N = 7Output: 6141004759 The approach for Ackermann function described in this 13 min read Divide and Conquer Optimization in Dynamic Programming Dynamic programming (DP) is arguably the most important tool in a competitive programmer's repertoire. There are several optimizations in DP that reduce the time complexity of standard DP procedures by a linear factor or more, such as Knuth's optimization, Divide and Conquer optimization, the Convex 15+ min read Convert a number m to n using minimum number of given operations Convert a number m to n with minimum operations. The operations allowed are : Multiply by 2, i.e., do m = 2 * mSubtract 1, i.e., do m = m - 1 Print -1 if it is not possible to convert.Examples : Input : m = 3, n = 11 Output : 3 1st operation: *2 = 3*2 = 6 2nd operation: *2 = 6*2 = 12 3rd operation: 7 min read Steps to solve a Dynamic Programming Problem Steps to solve a Dynamic programming problem:Identify if it is a Dynamic programming problem.Decide a state expression with the Least parameters.Formulate state and transition relationship.Apply tabulation or memorization.Step 1: How to classify a problem as a Dynamic Programming Problem? Typically, 13 min read Minimum number operations required to convert n to m | Set-2 Given two integers n and m and a and b, in a single operation n can be multiplied by either a or b. The task is to convert n to m with a minimum number of given operations. If it is impossible to convert n to m with the given operation then print -1. Examples: Input: n = 120, m = 51840, a = 2, b = 3 5 min read Like