Gamified challenge · Queues
n players numbered 1 to n sit in a circle in order, and player n passes to player 1. Player 1 starts holding the potato and counts as the 1st holder. The potato is passed round the circle, and the k-th holder is out: they leave the circle, and the next player round picks up the potato and counts as the 1st holder of a fresh count.
This repeats until one player is left. Write lastStanding(n, k) (Python: last_standing(n, k)) that returns that player's number. 1 ≤ n ≤ 100 and k ≥ 1; k may be larger than the circle, in which case the count simply goes round more than once.
With n = 5, k = 2, players go out in the order 2, 4, 1, 5, so the answer is 3.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google