top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Convert a number m to n with minimum operations. The operations allowed were -1 and *2.

+2 votes
3,101 views

convert a number m to n with minimum operations. The operations allowed were -1 and *2.

For Eg : 4 and 6. Answer is 2.
1st operation : -1 -> 4-1 = 3.
2nd operation : * -> 3 * 2 =6.

posted Jan 14, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Here is function to get value of minOperation

int convert(int m, int n) {
        if (m == n) {
            return 0;
        }
        if (m > n) {

            return m-n;
        }
        if (m <= 0 && n > 0) {
            return -1;
        }
        if (n % 2 == 1) {

            return 1 + convert(m, n+1);

        } else {
            return 1 + convert(m, n/2);
        }
    }
answer Feb 7, 2016 by Shivam Kumar Pandey
Similar Questions
+8 votes

Convert the given string into palindrome with minimum number of appends(at end of the given string). O(n) algorithm will be appreciated ??

Input :=> Malayal
Output :=> Malayalam

+3 votes

In an "N" element integer sorted array, a particular elements is repeating "(N/2)+1" times. How much time it take to find the repeating element.

...