Skip to content
Related Articles

Related Articles

Improve Article

Minimize steps to reach K from 0 by adding 1 or doubling at each step

  • Difficulty Level : Medium
  • Last Updated : 13 May, 2021

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:
Explanation: 
Step 1: 0 + 1 = 1 = K
Input: K = 4 
Output:
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
     

dp[i] =\begin{cases} dp[i - 1] + 1 & \text{; if i is odd} \\ dp[\frac{i}{2}] + 1 & \text{; if i is even} \end{cases}
 

  •  

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);
}

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

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

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

Javascript




<script>
// Javascript implementation of the above approach
 
// Function to find minimum operations
function minOperation(k)
{
       
    // dp is initialised
    // to store the steps
    let dp = Array.from({length: k+1}, (_, i) => 0);
   
    for(let 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   
    let K = 12;
    document.write( minOperation(K));
  
 // This code is contributed by target_2.
</script>
Output: 
5

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :