forked from kishanrajput23/leetcode-solutions-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjosh-solution.cpp
More file actions
39 lines (31 loc) · 802 Bytes
/
josh-solution.cpp
File metadata and controls
39 lines (31 loc) · 802 Bytes
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
#include <bits/stdc++.h>
using namespace std;
void Josh(vector<int> person, int k, int index)
{
// Base case , when only one person is left
if (person.size() == 1) {
cout << person[0] << endl;
return;
}
// find the index of first person which will die
index = ((index + k) % person.size());
// remove the first person which is going to be killed
person.erase(person.begin() + index);
// recursive call for n-1 persons
Josh(person, k, index);
}
int main()
{
int n = 14; // specific n and k values for original
// josephus problem
int k = 2;
k--; // (k-1)th person will be killed
int index
= 0; // The index where the person which will die
vector<int> person;
// fill the person vector
for (int i = 1; i <= n; i++) {
person.push_back(i);
}
Josh(person, k, index);
}