There are n people standing in a line labeled from 1 to n. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.

  • For example, once the pillow reaches the nth person they pass it to the n - 1th person, then to the n - 2th person and so on.

Given the two positive integers n and time, return the index of the person holding the pillow after time seconds.

Example 1:

Input: n = 4, time = 5 Output: 2 Explanation: People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2. Afer five seconds, the pillow is given to the 2nd person.

Example 2:

Input: n = 3, time = 2 Output: 3 Explanation: People pass the pillow in the following way: 1 -> 2 -> 3. Afer two seconds, the pillow is given to the 3rd person.

Constraints:

  • 2 <= n <= 1000
  • 1 <= time <= 1000

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: n, time, i, j

    do i = 1, size(examples)
        read (unit=examples(i), fmt='(I2, I2)') n, time
        write (*, '(A, I2, A, I2)') 'Example ', i, ': ', solve(n, time)
    end do

contains

    function solve(n, time) result(index)
        implicit none

        integer, intent(in) :: n, time
        integer :: index

        if (mod(time, 2) == 0) then
            ! Passing pillow in one direction
            index = mod((time + 1)/2, n) + 1
        else
            ! Passing pillow in opposite direction
            index = mod(n - (time + 1)/2, n) + 1
        end if

    end function solve

end program main

! Examples
integer, parameter :: examples(2, 2) = reshape([ &
    4, 5, &
    3, 2], shape(examples))
Compiled
Executed
Correct
! A Fortran script to solve the problem

! Declare variables
integer :: n, time, i, j, k, l

! Read input from stdin
read(*,*) n, time

! Initialize variables
i = 1
j = 2
k = 3
l = 4

! Loop through time
do while (i <= time)
    ! If i is odd, pass the pillow to the next person
    if (mod(i, 2) == 1) then
        if (j <= n) then
            j = j + 1
        else
            j = j - n
        end if
    ! If i is even, pass the pillow to the previous person
    else
        if (j >= 2) then
            j = j - 1
        else
            j = j + n
        end if
    end if

    ! Print the current state of the line
    write(*,*) "After", i, "seconds, the pillow is held by person", j

    ! Increment i
    i = i + 1
end do

! Print the final state of the line
write(*,*) "After", time, "seconds, the pillow is held by person", j

! End of the program
end
🌐 Data from online sources
def pillowHolder(n, time):
    time %= (2 * n - 2)
    if time <= n - 1:
        return time + 1
    else:
        return 2 * n - time - 1

The algorithm uses modulo to find the person holding the pillow after a given amount of time. First, find the time modulo 2 * n - 2 (since the cycle of passing the pillow back and forth will repeat every 2 * n - 2 seconds). If the resulting time is less than or equal to n - 1, then the current holder of the pillow is at index time + 1. If not, the current holder is at index 2 * n - time - 1. This accounts for the reverse direction in which the pillow is being passed back to the front of the line after reaching the end.

🌐 Data from online sources
int pillowHolder(int n, int time) {
    time %= (2 * n - 2);
    if (time <= n - 1) return time + 1;
    else return 2 * n - time - 1;
}

The algorithm uses modulo to find the person holding the pillow after a given amount of time. First, find the time modulo 2 * n - 2 (since the cycle of passing the pillow back and forth will repeat every 2 * n - 2 seconds). If the resulting time is less than or equal to n - 1, then the current holder of the pillow is at index time + 1. If not, the current holder is at index 2 * n - time - 1. This accounts for the reverse direction in which the pillow is being passed back to the front of the line after reaching the end.