Skip to content

Commit 981bd4a

Browse files
authored
Create Predict Winner of Circular Game (#347)
There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 <= i < n, and moving clockwise from the nth friend brings you to the 1st friend. The rules of the game are as follows: Start at the 1st friend. Count the next k friends in the clockwise direction including the friend you started at. The counting wraps around the circle and may count some friends more than once. The last friend you counted leaves the circle and loses the game. If there is still more than one friend in the circle, go back to step 2 starting from the friend immediately clockwise of the friend who just lost and repeat. Else, the last friend in the circle wins the game. Given the number of friends, n, and an integer k, return the winner of the game.
1 parent 7a2a549 commit 981bd4a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int solve(int n, int k)
5+
{
6+
if (n == 1)
7+
return 0;
8+
return (solve(n - 1, k) + k) % n;
9+
}
10+
11+
int predict(int n, int k)
12+
{
13+
int ans = solve(n, k);
14+
return ans + 1;
15+
}
16+
17+
int main()
18+
{
19+
int n, k;
20+
cin >> n >> k;
21+
cout << predict(n, k);
22+
return 0;
23+
}
24+
25+
// by YASH GAUTAM

0 commit comments

Comments
 (0)