A control panel has n indicator lamps arranged in a single row, numbered 0 to n-1 from left to right. At time 0, a status beacon lights lamp 0 and begins sweeping to the right at a rate of exactly one lamp per second. Whenever the beacon reaches either end of the row (lamp 0 or lamp n-1), it reverses direction on the very next second and continues sweeping the other way, bouncing back and forth indefinitely.
Given n and an elapsed time k (in seconds), determine the index of the lamp that is lit at time k.
A single line containing two space-separated integers n and k.
A single integer: the index of the lamp lit after k seconds.
Example 1
Input
3 4
Expected
0
Explanation
The beacon's position over time is: t=0 -> 0 (moving right), t=1 -> 1, t=2 -> 2 (hits the right end, reverses), t=3 -> 1, t=4 -> 0. So at k=4 the lit lamp is 0.
Example 2
Input
5 6
Expected
2
Explanation
The full bounce period is 2*(n-1)=8 seconds. Since k=6 is less than one full period, no wraparound is needed: the beacon reaches lamp 4 at t=4 and reverses, so it is at position 8-6=2 at t=6.
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 →