Pass the Pillow

n people stand in a line labeled 1 to n. Person 1 starts with a pillow, and every second the holder passes it to the next person. When the pillow reaches the end of the line, the direction reverses. Given n and time, return the label of the person holding the pillow after time seconds.

30:00

Pass the Pillow
easy
Free written solution
Topics
Companies

n people stand in a line labeled 1 to n. Person 1 starts with a pillow, and every second the holder passes it to the next person. When the pillow reaches the end of the line, the direction reverses. Given n and time, return the label of the person holding the pillow after time seconds.

Example 1:
Input: {"n":4,"time":5}
Output: 2
Constraints:
  • 2≤n≤10002 \leq n \leq 1000

  • 1≤time≤10001 \leq time \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":4,"time":5}

Person 1 holds the pillow at second 0. Walking from one end of the line to the other takes n - 1 = 3 passes.

the line at second 0 (pillow moving right →)

1
2
3
4

holder over time: each straight run is one trip of 3 seconds

second 0

second 5

trips = 5 // 3 = ?, rem = 5 % 3 = ?
Variables
VariableValue
n4
time5
DepthFunction Call
Stack empty
0/5