Minimum Operations | Practice | GeeksforGeeks

Given a number N. Find the minimum number of operations required to reach N starting from 0. You have 2 operations available:
Double the number 
Add one to the number

Example 1:
Input:
N = 8
Output: 4
Explanation: 0 + 1 = 1, 1 + 1 = 2,
2 * 2 = 4, 4 * 2 = 8


â€‹Example 2:
Input: 
N = 7
Output: 5
Explanation: 0 + 1 = 1, 1 + 1 = 2,
1 + 2 = 3, 3 * 2 = 6, 6 + 1 = 7


Your Task:
You don't need to read input or print anything. Your task is to complete the function minOperation() which accepts an integer N and return number of minimum operations required to reach N from 0.

Expected Time Complexity: O(LogN)
Expected Auxiliary Space: O(1)

Constraints:
1 <= N <= 106
0
shaank181851 minutes ago
Java Code
class Solution
{
    public int minOperation(int n)
    {
        int count = 0;
        //code here.
    while(n!=0){
        //if no is divisible by 2 then divide it by two else substract 1;
        if(n%2==0){
            n = n/2;
            count++;
          }
        else{
            n = n-1;
            count++;
        }
    }
        return count;
    }
        
}
0
mohitaggar20083 weeks ago
int minOperation(int n)   {       //code here.       int A[n+1],j,k,min=INT_MAX;       A[0]=0;       A[1]=1;       A[2]=2;       for(int i=3;i<n+1;++i)       {   min=INT_MAX;           A[i]=i;           j=i;           k=0;           while(k<j)            {                if(A[k]+A[j]<min)                  min=A[k]+A[j];                  j--;                  k++;                              }            A[i]=min;            if(j==k)              {                  if(A[j]+1<A[i])                    A[i]=A[j]+1;              }       }       return A[n];   }
solution using dp
but time limit exeeds 
 
0
moaslam8263 weeks ago
C++ EASY SOLN
int fun(int n){
      if(n==0){
          return 0;
      }
      if(n/2!=0 && !(n&1)){
          return 1+fun(n/2);
      }
      else{
          return 1+fun(n-1);
      }
  }
    int minOperation(int n)
    {
        //code here
        return fun(n);
    }
+1
kashyapjhon3 weeks ago
C++ Solution DP Code Time=(0.0/1.3):
int help(vector<int> &dp,int n){       if(n==0){           return 0;       }       if(dp[n]!=-1){           return dp[n];       }       if(n%2==0){           int l= help(dp,n-1)+1;           int r= help(dp,n/2)+1;           return dp[n]=min(l,r);       }       else{           return dp[n]=help(dp,n-1)+1;       }   }   int minOperation(int n)   {       //code here.       vector<int> dp(n+1,-1);       help(dp,n);       return dp[n];   }
+1
19501a05h21 month ago
simple C++ dp answer
 
int minOperation(int n)   {      int dp[n];      dp[0] = 0;      for(int i=1;i<=n;i++) {           if(i%2==0)              dp[i]=dp[i/2]+1;           else                dp[i] = dp[i-1]+1;      }      return dp[n];   }
0
mayank20211 month ago
C++ : 0.0/1.3int minOperation(int n)   {       int count=0;              while(n)       {                      if(n&1)                n=n-1;           else               n=n/2;           count++;       }                     return count;   }
0
cs19b01911 month ago
int dp[n+1];       for(int i = n ; i >= 1 ; i--)       {           if(i == n)            dp[i] = 0;           else           {               int temp1 = i+1;               int temp2 = i*2;               if(temp1 > n && temp2 > n)                dp[i] = 1+i;               else if(temp1 > n)                dp[i] = min(1+i,1+dp[temp2]);               else if(temp2 > n)                dp[i] = min(1+dp[temp1] , 1+i);               else                dp[i] = min(1+dp[temp1] , 1+dp[temp2]);                           }       }       return dp[1]+1;
0
tyagipratap1111 month ago
// Just for reference type variable
  class Count {
        private int a;

        public Count(int x) {
            a = x;
        }

        public void update(int x) {
            a = x;
        }

        public int get() {
            return a;
        }
    }

// Recursive function
    int find(int n, Count c) {
        if (n == 0)
            return 0;
        if (n == 1) {
            c.update(c.get() + 1);
            return 1;
        }

        if (n % 2 == 0)
            n = n / 2;
        else
            n = n - 1;

        c.update(c.get() + 1);

        return find(n, c);
    }

    int minOperation(int n) {
        Count c = new Count(0);
        find(n, c);
        return c.get();
    }
0
anshul gupta 62 months ago
WITH DP
 
int[] dp = new int[n + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 0; i < dp.length; i++) {
            int temp = i + 1;
            if (temp < dp.length)
                dp[temp] = Math.min(dp[i] + 1, dp[temp]);
            temp = i * 2;
            if (temp < dp.length)
                dp[temp] = Math.min(dp[i] + 1, dp[temp]);
        }
        return dp[n];
0
akkick1432 months ago
class Solution:
    def minOperation(self, n):
        if n<2:
            return n
        else:
            if n%2==1:
                res=1+self.minOperation(n-1)
            else:
                res=self.minOperation(n//2)+1
        return res
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.