Problem Description:
We have 2 numbers a and b as input. We can obtain b from a. In each move we can add any odd number or subtract any even number. We have to count the minimum move required to obtain b from a.
Ideological Analysis:
Case 1: If \(a = b\) then, no move is required. So, answer will be ‘0’.
Case 2: If \(a \le b\) then,
-
If the difference of a and b is odd then one move is required. Because we can add the difference with a \( (a + x) \) and obtain b.
For example, \(a = 2\) and \(b = 5\) , \(difference = b – a = 3\)
\(a + 3 = b\). So, the output will be ‘1’.
-
If the difference is even, then two moves are required. Because we can have even number by adding 2 odd numbers.
For example, \(a = 2\) and \(b = 6\), \(difference = b – a = 4\)
\(a + 3 + 1 = b\). \( (a + x + x) \) So, the output will be ‘2’.
Case 3: If \(a \ge b\) then, (opposite of Case 2)
-
if the difference of a and b is odd then two moves are required. \( (a + odd number – even number) \)
For example, \(a = 5\) and \(b = 2\) , \(difference = a – b = 3\)
\(a + 1 - 4 = b\). \( (a – x + y) \) So, the output will be ‘2’.
-
If the difference is even, then one move is required. Because we can subtract the difference from a \( (a – y) \) and obtain b.
For example, \(a = 6\) and \(b = 2\), \(difference = a – b = 4\)
\(a - 4 = b\). \( ( a – y ) \) So, the output will be ‘1’.
Click here for Source Code.