[Codewars] Josephus Permutation

Josephus Permutation

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <queue>
using namespace std;
std::vector < int> josephus(std::vector < int > items, int k) {
vector<int> res;
queue<int> q;
for(auto i : items) q.push(i);
while(q.size()) {
for(int i = 0; i < (k - 1) % q.size(); i++) {
q.push(q.front()); q.pop();
}
res.push_back(q.front()); q.pop();
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/04/30/PS/Codewars/josephus-permutation/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.