```
public class Solution {
public int lastRemaining(int n) {
return help(n, true);
}
private int help(int n, boolean left) {
if (n == 1) return 1;
int res = help(n / 2, !left) * 2;
if ((n & 1) == 1 || left) {
return res;
}
return res - 1;
}
}
```