Problem Statement
A group of people are standing in a circle, numbered from 1
to n
. Starting from a specific person, every k
th person in the circle is eliminated until only one person remains. The task is to determine the position of the last remaining person.
This is a classic problem in combinatorial mathematics, often referred to as the Josephus Problem.
Examples
Example 1:
Input: n = 7, k = 3
Output: 4
Explanation: The people are numbered from 1 to 7. Starting from 1 and counting every 3rd person, the elimination order is:
1. Eliminate person 3
2. Eliminate person 6
3. Eliminate person 2
4. Eliminate person 7
5. Eliminate person 5
6. Eliminate person 1.
The last person remaining is 4.
Example 2:
Input: n = 5, k = 2
Output: 3
Explanation:
With 5 people and counting every 2nd person, the elimination order is:
1. Eliminate person 2
2. Eliminate person 4
3. Eliminate person 1
4. Eliminate person 5
The last person remaining is 3.