Skip to content

Commit a7ce46b

Browse files
committed
feat(cpp): add josephus.cpp
1 parent 68517ab commit a7ce46b

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

cpp/Math and Geometry/josephus.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int josephus(int n, int k) {
2+
// Base case: If there's only one person, the last person is person 0.
3+
if (n == 1) {
4+
return 0;
5+
}
6+
// Calculate the position of the last person remaining in the reduced problem
7+
// with 'n - 1' people. We use modulo 'n' to ensure the answer doesn't exceed
8+
// 'n - 1'.
9+
return (josephus(n - 1, k) + k) % n;
10+
}

0 commit comments

Comments
 (0)