Skip to content
Related Articles
Convert N to M with given operations using dynamic programming
• Difficulty Level : Hard
• Last Updated : 09 Jun, 2021

Given two integers N and M and the task is to convert N to M with the following operations:

1. Multiply N by 2 i.e. N = N * 2.
2. Subtract 1 from N i.e. N = N – 1.

Examples:

Input: N = 4, M = 6
Output:
Perform operation 2: N = N – 1 = 4 – 1 = 3
Perform operation 1: N = N * 2 = 3 * 2 = 6
Input: N = 10, M = 1
Output:

Approach: Create an array dp[] of size MAX = 105 + 5 to store the answer in order to prevent 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 the minimum because if N is odd then it can be reached only by performing N – 1 operations and if N is even then 2 * N opearations have to be performed so check both the possibililties and return the minimum.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `const` `int` `N = 1e5 + 5;` `int` `n, m;``int` `dp[N];` `// Function to reutrn 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 opertaions``    ``// and If k is odd then it can be reached``    ``// by k-1 opertaions 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 reutrn 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 opertaions``        ``// and If k is odd then it can be reached``        ``// by k-1 opertaions 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 reutrn 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 opertaions``    ``# and If k is odd then it can be reached``    ``# by k-1 opertaions 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 reutrn 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 opertaions``        ``// and If k is odd then it can be reached``        ``// by k-1 opertaions 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

 ``
Output:
`2`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up