A relay drill lines up n runners in a row, numbered 1 to n from one end to the other. A baton starts with runner 1 and is passed to the adjacent runner once every second, moving toward higher numbers at first. The instant the baton would be passed off the end of the line (past runner n or back past runner 1), it instead bounces and the direction of passing reverses, continuing back and forth indefinitely. Given the total number of seconds that have elapsed, determine which runner is holding the baton at that moment.
A single line containing two integers n and time, separated by a space.
A single integer: the number of the runner holding the baton after time seconds have elapsed.
Example 1
Input
4 5
Expected
2
Explanation
With n = 4, the baton's position each second is 1 (start), 2, 3, 4 (hits the end and reverses), 3, 2. After time = 5 seconds it has moved through that sequence and lands on runner 2.
Example 2
Input
3 2
Expected
3
Explanation
With n = 3, the baton hasn't yet reached either end: position 1 (start), 2, 3. After time = 2 seconds it is at runner 3.
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 →