Based on this solution by @NathanNi. Set all even bits lower than n's high-bit (corresponds to his adding of `step`

in case of `left`

), and keep the odd bits lower than the high-bit (corresponds to his adding of `step`

in case of `remaining % 2 == 1`

). My `m`

is the bitmask of all bits lower than n's high-bit.

```
int lastRemaining(int n) {
int m = n >> 1;
m |= m >> 1;
m |= m >> 2;
m |= m >> 4;
m |= m >> 8;
m |= m >> 16;
return 1 + (0x55555555 & m | n & m);
}
```

Also check out @stachenov's awesome Java one-liner below.