Joseph ring problem.
Idea: Kill them one by one, and then make up for the restoring position in the reverse direction.
Reference explanation
int lastRemaining(int n, int m){
int pos = 0;
for(int i = 2; i <= n; i++){
pos = (pos + m) % i;
}
return pos;
}