|
1 | | -There are n people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom. Given the total number of persons n and a number k which indicates that k-1 persons are skipped and kth person is killed in circle. The task is to choose the place in the initial circle so that you are the last one remaining and so survive. |
| 1 | +There are `n` people standing in a circle waiting to be executed. The counting out begins |
| 2 | +at some point in the circle and proceeds around the circle in a fixed direction. In each |
| 3 | +step, a certain number of people are skipped and the next person is executed. The |
| 4 | +elimination proceeds around the circle (which is becoming smaller and smaller as the |
| 5 | +executed people are removed), until only the last person remains, who is given freedom. |
| 6 | +Given the total number of persons `n` and a number `k` (the step count) which indicates |
| 7 | +that `k-1` persons are skipped and `k`th person is killed in circle. The task is to find |
| 8 | +out which person will survive. |
2 | 9 |
|
3 | 10 | ### Example |
4 | 11 |
|
5 | | -__Input:__ |
6 | | - |
7 | | -n = 5 (total number of people in circle) |
8 | | - |
9 | | -k = 2 (number of people - 1 are skipped and kth person is killed) |
10 | | - |
11 | | -__Output__: (Number of people in the circle currently, number of people to skip, index of the person to be killed or removed) |
12 | | - |
13 | | -[1, 2, 3, 4, 5] 1 0 |
14 | | - |
15 | | -[1, 3, 4, 5] 1 1 |
16 | | - |
17 | | -[1, 3, 5] 1 2 |
18 | | - |
19 | | -[3, 5] 1 0 |
20 | | - |
21 | | -3 |
22 | | - |
23 | | -At the end, 3rd person stays alive. |
| 12 | +In this example, 5 people are placed in a circle (`n = 5`), and the step count is 2 (`k = 2`). |
| 13 | + |
| 14 | +<pre> |
| 15 | + [1] |
| 16 | + / \ |
| 17 | + [5] [2] |
| 18 | + | | |
| 19 | + [4]-----[3] |
| 20 | +</pre> |
| 21 | + |
| 22 | +The count starts at person 1, and person 2 is executed: |
| 23 | + |
| 24 | +<pre> |
| 25 | + [1] |
| 26 | + / \ |
| 27 | + [5] <span style="color: red">[2] X</span> |
| 28 | + | | |
| 29 | + [4]-----[3] |
| 30 | +</pre> |
| 31 | + |
| 32 | +The count now starts at person 3, and person 4 is executed: |
| 33 | + |
| 34 | +<pre> |
| 35 | + [1] |
| 36 | + / \ |
| 37 | + [5] <span style="color: red;">[2] X</span> |
| 38 | + | | |
| 39 | +<span style="color: red;">X [4]</span>-----[3] |
| 40 | +</pre> |
| 41 | + |
| 42 | +The count now starts a person 5, and person 1 is executed: |
| 43 | + |
| 44 | +<pre> |
| 45 | + <span style="color: red;">[1] X</span> |
| 46 | + / \ |
| 47 | + [5] <span style="color: red;">[2] X</span> |
| 48 | + | | |
| 49 | +<span style="color: red;">X [4]</span>-----[3] |
| 50 | +</pre> |
| 51 | + |
| 52 | +The count now starts at person 3, and person 5 is executed: |
| 53 | + |
| 54 | +<pre> |
| 55 | + <span style="color: red;">[1] X</span> |
| 56 | + / \ |
| 57 | +<span style="color: red;">X [5]</span> <span style="color: red;">[2] X</span> |
| 58 | + | | |
| 59 | +<span style="color: red;">X [4]</span>-----[3] |
| 60 | +</pre> |
| 61 | + |
| 62 | +At the end, person 3 is the survivor. |
0 commit comments