A garage tests two mechanically linked trip odometers side by side. During every calibration cycle, the technician can turn each odometer forward or backward by exactly one unit, choosing each odometer's direction independently (the two need not move the same way in a given cycle). Given the reference odometer's current reading and a fixed budget of calibration cycles, find the largest possible starting reading for the second odometer such that some sequence of at most that many cycles can bring both odometers to display the exact same number.
A single line containing two space-separated integers num and t: the reference odometer's current reading and the maximum number of calibration cycles available.
Print a single integer: the maximum possible starting reading for the second odometer such that it can still be brought to match the reference odometer's reading using at most t cycles.
Example 1
Input
4 1
Expected
6
Explanation
With one calibration cycle, the second odometer can start at 6 and move down by 1 to 5, while the reference odometer (starting at 4) moves up by 1 to 5 as well — both end at 5. Starting any higher than 6 would leave a gap that a single cycle (which can close the gap by at most 2) cannot bridge, so 6 is the maximum.
Example 2
Input
1 2
Expected
5
Explanation
With two calibration cycles the gap can close by at most 4. Starting the second odometer at 5, it moves down by 1 each cycle (5→4→3) while the reference odometer (starting at 1) moves up by 1 each cycle (1→2→3); both end at 3. Any higher starting value would leave a gap too large to close in 2 cycles.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →