1700. Passing Parcel Eliminations

Medium3.5 points estimatedQueueSimulationArray

Players numbered 0 to n-1 sit in a circle for a game of pass-the-parcel, and player 0 holds the parcel at the start. Each round, the parcel is passed along the circle k mod s times, where s is the number of players still in the game; the player holding it then is eliminated, and k becomes cards[p], the card of the eliminated player p. The next player clockwise from the eliminated one now holds the parcel and the next round starts. Return the player numbers in the order they were eliminated, as an int[]. The initial value of k is the second argument, and passing zero times is allowed. O(n^2) time is fine for the limits.

Example 1

Input:
cards = [1,2,3,4], k = 1
Output:
[1,0,3,2]
Explanation:

Rotate once and eliminate player 1 (new k = 2), then eliminate player 0 (k = 1), player 3 (k = 4), and finally player 2.

Example 2

Input:
cards = [5], k = 0
Output:
[0]
Explanation:

A single player is eliminated at once.

Example 3

Input:
cards = [0,0,0], k = 0
Output:
[0,1,2]
Explanation:

With k = 0 and all cards 0 nobody ever rotates, so the players leave in seating order.

Constraints

1 ≤ n ≤ 1000

0 ≤ k ≤ 109

0 ≤ cards[i] ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n^2)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…