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.
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. After five seconds, the 2nd person is holding the pillow.
Example 2:
Input: n = 3, time = 2 Output: 3 Explanation: People pass the pillow in the following way: 1 -> 2 -> 3. After two seconds, the 3rd person is holding the pillow.
Constraints:
2 <= n <= 1000
1 <= time <= 1000
Note: This question is the same as 3178: Find the Child Who Has the Ball After K Seconds.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to solve this problem is like simulating the pillow being passed back and forth, counting each time. We will manually simulate the pillow's journey until the specified number of passes have been made.
Here's how the algorithm would work step-by-step:
def pass_the_pillow_brute_force(number_of_people, number_of_passes):
current_position = 1
direction = 1 # 1 for forward, -1 for backward
for _ in range(number_of_passes):
current_position += direction
# We've reached the end, time to go back
if current_position > number_of_people:
current_position = number_of_people - 1
direction = -1
# We've reached the start, time to go forward
elif current_position < 1:
current_position = 2
direction = 1
return current_position
The most efficient way to solve this problem involves understanding the pattern of movement back and forth. Instead of simulating each pass of the pillow, we can directly calculate where the pillow will be after a certain number of passes by using some math related to cycles.
Here's how the algorithm would work step-by-step:
def pass_the_pillow(number_of_people, number_of_passes):
round_trip_length = (number_of_people - 1) * 2
number_of_remaining_passes = number_of_passes % round_trip_length
# Determine if the pillow is moving forward or backward
if number_of_remaining_passes <= number_of_people - 1:
pillow_position = 1 + number_of_remaining_passes
else:
# Account for the 'backward' part of the cycle
pillow_position = number_of_people - (number_of_remaining_passes - (number_of_people - 1))
return pillow_position
Case | How to Handle |
---|---|
n = 1 (only one person) | Always return 1, as the pillow stays with the first person |
time = 0 (no passing occurs) | Return 1, as the pillow starts with the first person |
time is a large number causing integer overflow in intermediate calculations | Use long data type for 'time' to avoid potential overflow during calculation |
time is exactly divisible by (n-1)*2 | The pillow returns to the starting position 1, so handle the case when the cycle completes an exact number of times |
time is close to the maximum integer value | Ensure intermediate calculations don't overflow even with 'long' data type |
n is a small number, such as 2 | Handle the case of two people, where the direction changes every time |
n is equal to Integer.MAX_VALUE (extreme boundary value for number of positions) | The solution may not be efficient due to calculations involving large numbers, consider using modular arithmetic appropriately |
n is a negative number or zero. | Return an error or throw an exception as 'n' represents a count of people and cannot be non-positive. |