Open In App

Convert N to M with given operations using dynamic programming

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 the same computation again and again and initialize all the array elements with -1.  

Below is the implementation of the above approach: 




// 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 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 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# 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




<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

 


Article Tags :