class Solution {
public:
int lastRemaining(int n, int m){
int f[n];
memset(f, 0, sizeof f);
int remain = n;
int i = 0, j = i;
while (remain-- > 1)
{
int cnt = 0;
while (cnt < m)
{
j = i % n;
if (f[j])
{
i++;
continue;
}
cnt++;
i++;
}
f[j] = 1;
i = j + 1;
}
for (int i = 0; i < n; i++)
if (!f[i]) return i;
}
};