Minimize operations required to obtain N

Given an integer N, the task is to obtain N, starting from 1 using minimum number of operations. The operations that can be performed in one step are as follows:
- Multiply the number by 2.
- Multiply the number by 3.
- Add 1 to the number.
Explanation:Â
Input: N = 5Â
Output: 3Â
Explanation:Â
Starting value: x = 1
- Multiply x by 2 : x = 2
- Multiply x by 2 : x = 4
- Add 1 to x : x = 5
Therefore, the minimum number of operations required = 3
Input: N = 15Â
Output: 4Â
Explanation:Â
3 operations required to obtain x = 5.Â
Multiply x by 3 : x = 15.Â
Therefore, the minimum number of operations required = 4Â
Â
Approach:Â
The idea is to use the concept of Dynamic Programming. Follow the steps below:
- If minimum operations to obtain any number smaller than N is known, then minimum operations to obtain N can be calculated.
- Create the following lookup table:
 dp[i] = Minimum number of operations to obtain i from 1Â
- So for any number x, minimum operations required to obtain x can be calculated as:
 dp[x] = min(dp[x-1], dp[x/2], dp[x/3])
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>using namespace std;Â
// Function to find the minimum number// of operationsint minOperations(int n){    // Storing the minimum operations    // to obtain all numbers up to N    int dp[n + 1];Â
    // Initial state    dp[1] = 0;Â
    // Iterate for the remaining numbers    for (int i = 2; i <= n; i++) {Â
        dp[i] = INT_MAX;Â
        // If the number can be obtained        // by multiplication by 2        if (i % 2 == 0) {            int x = dp[i / 2];            if (x + 1 < dp[i]) {                dp[i] = x + 1;            }        }        // If the number can be obtained        // by multiplication by 3        if (i % 3 == 0) {            int x = dp[i / 3];            if (x + 1 < dp[i]) {                dp[i] = x + 1;            }        }Â
        // Obtaining the number by adding 1        int x = dp[i - 1];        if (x + 1 < dp[i]) {            dp[i] = x + 1;        }    }Â
    // Return the minm operations    // to obtain n    return dp[n];}Â
// Driver Codeint main(){Â Â Â Â int n = 15;Â Â Â Â cout << minOperations(n);Â Â Â Â return 0;} |
Java
class GFG{     // Function to find the minimum number// of operationsstatic int minOperations(int n){         // Storing the minimum operations    // to obtain all numbers up to N    int dp[] = new int[n + 1];Â
    // Initial state    dp[1] = 0;Â
    // Iterate for the remaining numbers    for(int i = 2; i <= n; i++)    {        dp[i] = Integer.MAX_VALUE;Â
        // If the number can be obtained        // by multiplication by 2        if (i % 2 == 0)        {            int x = dp[i / 2];            if (x + 1 < dp[i])             {                dp[i] = x + 1;            }        }                 // If the number can be obtained        // by multiplication by 3        if (i % 3 == 0)        {            int x = dp[i / 3];            if (x + 1 < dp[i])             {                dp[i] = x + 1;            }        }Â
        // Obtaining the number by adding 1        int x = dp[i - 1];        if (x + 1 < dp[i])         {            dp[i] = x + 1;        }    }Â
    // Return the minm operations    // to obtain n    return dp[n];}Â
// Driver Codepublic static void main (String []args){Â Â Â Â int n = 15;Â Â Â Â Â Â Â Â Â System.out.print( minOperations(n));}}Â
// This code is contributed by chitranayal |
Python3
import sysÂ
# Function to find the minimum number # of operations def minOperations(n):         # Storing the minimum operations     # to obtain all numbers up to N     dp = [sys.maxsize] * (n + 1)         # Initial state    dp[1] = 0         # Iterate for the remaining numbers     for i in range(2, n + 1):                 # If the number can be obtained         # by multiplication by 2        if i % 2 == 0:            x = dp[i // 2]            if x + 1 < dp[i]:                dp[i] = x + 1                 # If the number can be obtained         # by multiplication by 3         if i % 3 == 0:            x = dp[i // 3]            if x + 1 < dp[i]:                dp[i] = x + 1                         # Obtaining the number by adding 1        x = dp[i - 1]        if x + 1 < dp[i]:            dp[i] = x + 1                 # Return the minimum operations    # to obtain n    return dp[n]Â
# Driver coden = 15Â
print(minOperations(n))Â Â Â Â Â Â Â Â Â # This code is contributed by Stuti Pathak |
C#
using System;class GFG{      // Function to find the minimum number// of operationsstatic int minOperations(int n){          // Storing the minimum operations    // to obtain all numbers up to N    int []dp = new int[n + 1];      int x;    // Initial state    dp[1] = 0;      // Iterate for the remaining numbers    for(int i = 2; i <= n; i++)    {        dp[i] = int.MaxValue;          // If the number can be obtained        // by multiplication by 2        if (i % 2 == 0)        {            x = dp[i / 2];            if (x + 1 < dp[i])             {                dp[i] = x + 1;            }        }                  // If the number can be obtained        // by multiplication by 3        if (i % 3 == 0)        {            x = dp[i / 3];            if (x + 1 < dp[i])             {                dp[i] = x + 1;            }        }          // Obtaining the number by adding 1        x = dp[i - 1];        if (x + 1 < dp[i])         {            dp[i] = x + 1;        }    }      // Return the minm operations    // to obtain n    return dp[n];}  // Driver Codepublic static void Main (string []args){    int n = 15;          Console.Write(minOperations(n));}}  // This code is contributed by rock_cool |
Javascript
<script>Â
// Function to find the minimum number// of operationsfunction minOperations(n){         // Storing the minimum operations    // to obtain all numbers up to N    let dp = new Array(n + 1);Â
    // Initial state    dp[1] = 0;Â
    // Iterate for the remaining numbers    for(let i = 2; i <= n; i++)     {        dp[i] = Number.MAX_VALUE;Â
        // If the number can be obtained        // by multiplication by 2        if (i % 2 == 0)        {            let x = dp[parseInt(i / 2, 10)];                         if (x + 1 < dp[i])            {                dp[i] = x + 1;            }        }                 // If the number can be obtained        // by multiplication by 3        if (i % 3 == 0)         {            let x = dp[parseInt(i / 3, 10)];            if (x + 1 < dp[i])            {                dp[i] = x + 1;            }        }Â
        // Obtaining the number by adding 1        let x = dp[i - 1];                 if (x + 1 < dp[i])         {            dp[i] = x + 1;        }    }Â
    // Return the minm operations    // to obtain n    return dp[n];}Â
// Driver codelet n = 15;Â
document.write(minOperations(n));Â
// This code is contributed by divyesh072019Â
</script> |
4
Â
Time Complexity: O(N)Â
Auxiliary Space: O(N)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



