public int lastRemaining(int n) {

if (n == 1) {

return 1;

}

return (n / 2 + 1 - lastRemaining(n / 2)) * 2;

}

- After every step, we actually boil down the n problem to n / 2 problem, but in opposite direction;
- Last remain of n from left to right + last remaining of n from right to left = n + 1.