Vasya has found a strange device. On the front panel of a device there are: a re
ID: 3844015 • Letter: V
Question
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?
Input The first and the only line of the input contains two distinct integers n and m (1n,m104), separated by a space .
Output Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n. Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.
Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?
Input The first and the only line of the input contains two distinct integers n and m (1n,m104), separated by a space .
Output Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n. Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.
Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?
Input The first and the only line of the input contains two distinct integers n and m (1n,m104), separated by a space .
Output Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n.
Explanation / Answer
So in question it is given that we can acheive m by multiplying n by 2 or substracting 1 whichever is optimal. One point to note that multiplying by 2 is always going to make n even. Let us think it in a reverse way, can we make m to n by dividing m by 2 or adding 1. Let us take an example where m=99 and n =5, m>n. One can think lfollowing minimal steps to acheive that in reverse way as following
99/2 = 49
49/2 = 24
24/2 = 12
12/2 = 6
6/2 = 3
3+1+1 =5
Total steps = 7
Let us cross check it reverse way to prove it works
Now going from 5 to 99 we multiply by 2 or substract 1 so as follows below :
5-1-1 =3
3*2 =6
6*2 =12
12*2 =24
24*2 = 48
48*2 = 98
Now we stuck at 98 as we can't go further. So our previous logic needs is not correct because on multiplying we only get even numbers but dividing by 2 can give odd or even number. So we should make odd number first even by adding and then divide by 2 to get only even numbers so in reverse process we will get same result.
So here there are two possibilites either add 1 (label as type 1) or we divide by 2 (label as type 2). But only dividing by 2 makes us skipping the odd numbers. So suppose at some point we perform two operations of type 1 and one operation of type 2; but in this case one operation of type 2 and one operation of type 1 would lead to same result and sequence contain less operations then before. This implies that in an optimal answer more than one consecutive operation of type 1 is possible only idf no opertations of type 2 follow. So we divide by n by 2 if it is even, or add 1 and then divide by 2 if it is odd.
Now we modify our test case break down
n=5, m= 99
(99+1)/2 = 50
(50/2) = 25
(25+1)/2 =13
(13+1)/2 = 7
(7+1)/2 = 4
4+1 = 5
Now If you traverse reverse you will actually end up in getting 99 from 5.
What if n>m like suppose n =99 and m=5. So we can't multiply now as it will make n bigger what we can do is substraction only. Or in reverse way addition. Simply we can count ways by doing (n-m).
So whole Logic can be summarised as below
int minop(int n, int m)
{
int ans = 0;
if(n>=m)
{
ans+=n-m;
}
else
{
while(m>n)
{
if(m%2==0)
{
m/=2;
ans++;
}
else
{
ans++;
m++;
}
}
}
}
Time Complexity is O(Logn)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.