Given a positive integer **K**, the task is to find the minimum number of operations of the following two types, required to change 0 to K:

- Add one to the operand
- Multiply the operand by 2.

**Examples:**

Input:K = 1

Output:1

Explanation:

Step 1: 0 + 1 = 1 = K

Input:K = 4

Output:3

Explanation:

Step 1: 0 + 1 = 1,

Step 2: 1 * 2 = 2,

Step 3: 2 * 2 = 4 = K

**Approach:**

- If
**K**is an odd number, the last step must be adding 1 to it. - If
**K**is an even number, the last step is to multiply by 2 to minimise the number of steps. - Create a dp[] table that stores in every
**dp[i]**, the minimum steps required to reach**i**.

Below is the implementation of the above approach:

## C++

`// C++ program to implement the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find minimum operations ` `int` `minOperation(` `int` `k) ` `{ ` ` ` `// vector dp is initialised ` ` ` `// to store the steps ` ` ` `vector<` `int` `> dp(k + 1, 0); ` ` ` ` ` `for` `(` `int` `i = 1; i <= k; i++) { ` ` ` ` ` `dp[i] = dp[i - 1] + 1; ` ` ` ` ` `// For all even numbers ` ` ` `if` `(i % 2 == 0) { ` ` ` `dp[i] ` ` ` `= min(dp[i], ` ` ` `dp[i / 2] + 1); ` ` ` `} ` ` ` `} ` ` ` `return` `dp[k]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `K = 12; ` ` ` `cout << minOperation(k); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement the above approach ` `class` `GFG{ ` ` ` `// Function to find minimum operations ` `static` `int` `minOperation(` `int` `k) ` `{ ` ` ` ` ` `// dp is initialised ` ` ` `// to store the steps ` ` ` `int` `dp[] = ` `new` `int` `[k + ` `1` `]; ` ` ` ` ` `for` `(` `int` `i = ` `1` `; i <= k; i++) ` ` ` `{ ` ` ` `dp[i] = dp[i - ` `1` `] + ` `1` `; ` ` ` ` ` `// For all even numbers ` ` ` `if` `(i % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `dp[i] = Math.min(dp[i], dp[i / ` `2` `] + ` `1` `); ` ` ` `} ` ` ` `} ` ` ` `return` `dp[k]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String []args) ` `{ ` ` ` `int` `K = ` `12` `; ` ` ` `System.out.print( minOperation(K)); ` `} ` `} ` ` ` `// This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement the above approach ` ` ` `# Function to find minimum operations ` `def` `minOperation(k): ` ` ` ` ` `# dp is initialised ` ` ` `# to store the steps ` ` ` `dp ` `=` `[` `0` `] ` `*` `(k ` `+` `1` `) ` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, k ` `+` `1` `): ` ` ` `dp[i] ` `=` `dp[i ` `-` `1` `] ` `+` `1` ` ` ` ` `# For all even numbers ` ` ` `if` `(i ` `%` `2` `=` `=` `0` `): ` ` ` `dp[i]` `=` `min` `(dp[i], dp[i ` `/` `/` `2` `] ` `+` `1` `) ` ` ` ` ` `return` `dp[k] ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `k ` `=` `12` ` ` ` ` `print` `(minOperation(k)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to find minimum operations ` `static` `int` `minOperation(` `int` `k) ` `{ ` ` ` ` ` `// dp is initialised ` ` ` `// to store the steps ` ` ` `int` `[]dp = ` `new` `int` `[k + 1]; ` ` ` ` ` `for` `(` `int` `i = 1; i <= k; i++) ` ` ` `{ ` ` ` `dp[i] = dp[i - 1] + 1; ` ` ` ` ` `// For all even numbers ` ` ` `if` `(i % 2 == 0) ` ` ` `{ ` ` ` `dp[i] = Math.Min(dp[i], dp[i / 2] + 1); ` ` ` `} ` ` ` `} ` ` ` `return` `dp[k]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `K = 12; ` ` ` `Console.Write(minOperation(K)); ` `} ` `} ` ` ` `// This code is contributed by Nidhi_Biet ` |

*chevron_right*

*filter_none*

**Output:**

5

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:

- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Minimize the number of steps required to reach the end of the array
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimum step to reach one
- Reach A and B by multiplying them with K and K^2 at every step
- Count ways to reach the nth stair using step 1, 2 or 3
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Count ways to reach the Nth stair using any step from the given array
- Count ways to reach end from start stone with at most K jumps at each step
- Find the number of ways to reach Kth step in stair case
- Number of steps to sort the array by changing order of three elements in each step
- Minimum steps to minimize n as per given condition
- Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
- Minimize steps required to move all 1's in a matrix to a given index
- Minimum steps to reach a destination
- Minimize Steps required to obtain Sorted Order of an Array
- Count the minimum steps to reach 0 from the given integer N
- Minimum steps to reach target by a Knight | Set 2

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.