Broken Calculator c++ O(N) 0ms Fully expalined with dry run
174
here wewill go from target to startvalue
instead of multiply we will divide and substract=>addition

now we have
three case 
1.)if(start==target)return 0;
2.)target>start
               then we will keep dividing until target==start or start>target
               we divide only when the target is even else we will add 1
              
              
3.) start>target
         in this case we have only one option addtion to number of steps required is how much distance between them start-target
         
        // ex=>  start=3,target=10
         target>start  case 2 and it is even so divide it by 2until equal to start or lesser
         so target=5 start=3
         now still target is greater but we will only add 1 to it so target=6
         now we will divide by 2 since it is even so target=3 which is equivlaent to start so number of steps is 3*/

int brokenCalc(int startValue, int target) {
    int ans=0;
    while(target>startValue){
        ans++;
        if(target%2==0)
            target=target/2;
        else
            target=target+1;
    }
    return ans+startValue-target;
}

Comments (0)