You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/projects/josephus-problem/index.md
+60-20Lines changed: 60 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,32 +24,72 @@ Welcome to the Josephus Problem page! Here, you'll find a description of the pro
24
24
This article was written by:
25
25
26
26
- Jeremy Grifski
27
+
- rzuckerm
27
28
28
29
## Description
29
30
30
-
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.
31
+
There are `n` people standing in a circle waiting to be executed. The counting out begins
32
+
at some point in the circle and proceeds around the circle in a fixed direction. In each
33
+
step, a certain number of people are skipped and the next person is executed. The
34
+
elimination proceeds around the circle (which is becoming smaller and smaller as the
35
+
executed people are removed), until only the last person remains, who is given freedom.
36
+
Given the total number of persons `n` and a number `k` (the step count) which indicates
37
+
that `k-1` persons are skipped and `k`th person is killed in circle. The task is to find
38
+
out which person will survive.
31
39
32
40
### Example
33
41
34
-
__Input:__
35
-
36
-
n = 5 (total number of people in circle)
37
-
38
-
k = 2 (number of people - 1 are skipped and kth person is killed)
39
-
40
-
__Output__: (Number of people in the circle currently, number of people to skip, index of the person to be killed or removed)
41
-
42
-
[1, 2, 3, 4, 5] 1 0
43
-
44
-
[1, 3, 4, 5] 1 1
45
-
46
-
[1, 3, 5] 1 2
47
-
48
-
[3, 5] 1 0
49
-
50
-
3
51
-
52
-
At the end, 3rd person stays alive.
42
+
In this example, 5 people are placed in a circle (`n = 5`), and the step count is 2 (`k = 2`).
43
+
44
+
<pre>
45
+
[1]
46
+
/ \
47
+
[5] [2]
48
+
| |
49
+
[4]-----[3]
50
+
</pre>
51
+
52
+
The count starts at person 1, and person 2 is executed:
53
+
54
+
<pre>
55
+
[1]
56
+
/ \
57
+
[5] <spanstyle="color: red">[2] X</span>
58
+
| |
59
+
[4]-----[3]
60
+
</pre>
61
+
62
+
The count now starts at person 3, and person 4 is executed:
63
+
64
+
<pre>
65
+
[1]
66
+
/ \
67
+
[5] <spanstyle="color: red;">[2] X</span>
68
+
| |
69
+
<spanstyle="color: red;">X [4]</span>-----[3]
70
+
</pre>
71
+
72
+
The count now starts a person 5, and person 1 is executed:
73
+
74
+
<pre>
75
+
<spanstyle="color: red;">[1] X</span>
76
+
/ \
77
+
[5] <spanstyle="color: red;">[2] X</span>
78
+
| |
79
+
<spanstyle="color: red;">X [4]</span>-----[3]
80
+
</pre>
81
+
82
+
The count now starts at person 3, and person 5 is executed:
0 commit comments