-
Notifications
You must be signed in to change notification settings - Fork 617
/
josephus_problem.cpp
61 lines (48 loc) · 1.14 KB
/
josephus_problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
This is a theoretical computer science problem.
There are n people in a circle waiting to be executed.
Starting from any position, a person starts executing the k'th person ahead of him,
then the person after the one who is executed repeats the same task,
as there will be only one person surviving, we have to find the person who will remain alive.
*/
#include <iostream>
#include <vector>
using namespace std;
void josephus(vector<int> &v, int k, int idx, int &ans)
{
if (v.size() == 1)
{ // only one person will be left, which is the answer
ans = v[0];
return;
}
idx = (idx + k - 1) % v.size();
v.erase(v.begin() + idx);
josephus(v, k, idx, ans);
}
int main()
{
// use vector, if persons are not listed from 1 to n
vector<int> lst;
int n, k, ans = 0;
cin >> n >> k;
lst.reserve(n);
for (int i = 0; i < n; i++)
{
lst.push_back(i + 1);
}
// lst = {1,2,3,4,..,n}
josephus(lst, k, 0, ans);
cout << ans << endl;
return 0;
}
/*
Test Case:
Input: 3 2
Output: 3
Input: 4 6
Output: 3
Input: 32 32
Output: 27
Time Complexity: O(n)
Space Complexity: O(n)
*/