Skip to content

Commit 8512d8c

Browse files
authored
Merge pull request #508 from i-priyanshu/i-priyanshu
Added Josepheus Problem fixes #391
2 parents 846a4a4 + ed46902 commit 8512d8c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Given the total number of persons n and a number k,
2+
// which indicates that k-1 persons are skipped and kth person is killed in circle in a fixed direction.
3+
4+
// The task is to choose the safe place in the circle so that when you perform these operations starting from 1st place in the circle,
5+
// you are the last one remaining and survive.
6+
7+
#include <bits/stdc++.h>
8+
using namespace std;
9+
10+
void solve(vector<int> vec, int index, int k, int &ans)
11+
{ // Base Case
12+
if (vec.size() == 1)
13+
{
14+
ans = vec[0];
15+
return;
16+
}
17+
18+
index = (index + k) % vec.size(); // covering the case when index overflows the array.
19+
vec.erase(vec.begin() + index); // removing the Kth element
20+
solve(vec, index, k, ans); // Recursive call
21+
}
22+
23+
int main()
24+
{
25+
int n = 3, k = 2;
26+
vector<int> v;
27+
k--;
28+
int ans;
29+
for (int i = 1; i <= n; i++)
30+
{
31+
v.push_back(i);
32+
}
33+
solve(v, 0, k, ans);
34+
cout << ans;
35+
return ans;
36+
}

0 commit comments

Comments
 (0)