Josephus Survivor

MediumMathRecursionSimulation

Description

n people stand in a circle numbered 1..n. Starting from person 1 and counting around, every k-th person is eliminated. Return the 1-indexed position of the last remaining person.

Examples

Input:n = 5, k = 2
Output:3
Explanation:

Classic Josephus, k=2 eliminates 2,4,1,5 leaving 3.

Input:n = 1, k = 1
Output:1
Explanation:

Only one person.

Input:n = 7, k = 3
Output:4
Explanation:

Standard Josephus walk.

Constraints

  • 1 ≤ n ≤ 10⁵
  • 1 ≤ k ≤ 10⁵

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.