Problem · Math
Last Candidate Standing
Learn this problemProblem statement
Candidates numbered 1 through n stand in a circle. Starting at candidate 1, repeatedly count k remaining candidates, including the current candidate as count one. Eliminate the candidate counted as k, then continue from the next remaining candidate.
Return the number of the final candidate.
Function
lastCandidateStanding(n: int, k: int) → intExamples
Example 1
n = 5k = 2return = 3Candidates 2, 4, 1, and 5 are eliminated, leaving 3.
Example 2
n = 7k = 3return = 4The standard Josephus recurrence leaves candidate 4.
Constraints
1 <= n <= 100000001 <= k <= 10^9