[Codewars] Josephus Survivor

Josephus Survivor

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
#include <bits/stdc++.h>
using namespace std;
int josephusSurvivor(int n, int k) {
int res = 0;
for(int i = 1; i <= n; i++) {
res = (res + k) % i;
}
return res + 1;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/04/30/PS/Codewars/josephus-survivor/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.