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 = 5Output:3Explanation:

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 = 15Output:4Explanation:

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 operations` `int` `minOperations(` `int` `n)` `{` ` ` `// Storing the minimum operations` ` ` `// to obtain all numbers up to N` ` ` `int` `dp[n + 1];` ` ` `// Initilal 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 Code` `int` `main()` `{` ` ` `int` `n = 15;` ` ` `cout << minOperations(n);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`class` `GFG{` ` ` `// Function to find the minimum number` `// of operations` `static` `int` `minOperations(` `int` `n)` `{` ` ` ` ` `// Storing the minimum operations` ` ` `// to obtain all numbers up to N` ` ` `int` `dp[] = ` `new` `int` `[n + ` `1` `];` ` ` `// Initilal 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 Code` `public` `static` `void` `main (String []args)` `{` ` ` `int` `n = ` `15` `;` ` ` ` ` `System.out.print( minOperations(n));` `}` `}` `// This code is contributed by chitranayal` |

*chevron_right*

*filter_none*

## 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 code` `n ` `=` `15` `print` `(minOperations(n))` ` ` `# This code is contributed by Stuti Pathak` |

*chevron_right*

*filter_none*

## C#

`using` `System;` `class` `GFG{` ` ` `// Function to find the minimum number` `// of operations` `static` `int` `minOperations(` `int` `n)` `{` ` ` ` ` `// Storing the minimum operations` ` ` `// to obtain all numbers up to N` ` ` `int` `[]dp = ` `new` `int` `[n + 1];` ` ` ` ` `int` `x;` ` ` `// Initilal 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 Code` `public` `static` `void` `Main (` `string` `[]args)` `{` ` ` `int` `n = 15;` ` ` ` ` `Console.Write(minOperations(n));` `}` `}` ` ` `// This code is contributed by rock_cool` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(N) **Auxiliary Space:** O(N)

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.

## Recommended Posts:

- Minimize Steps required to obtain Sorted Order of an Array
- Minimum number of steps required to obtain the given Array by the given operations
- Minimum number of operations required to obtain a given Binary String
- Minimize count of divisions by D to obtain at least K equal array elements
- Minimize the value of N by applying the given operations
- Minimize cost to convert given two integers to zero using given operations
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimize the number of steps required to reach the end of the array
- Minimize steps required to move all 1's in a matrix to a given index
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Count operations of the given type required to reduce N to 0
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Minimum number operations required to convert n to m | Set-2
- Minimum number of operations required to reduce N to 1
- Minimum operations required to make two numbers equal
- Minimum number of operations required to sum to binary string S
- Count the number of carry operations required to add two numbers
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Minimize prize count required such that smaller value gets less prize in an adjacent pair
- Minimize number of cuts required to break N length stick into N unit length sticks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.