Description


Submission
class Solution {
public:
int lastRemaining(int n) {
bool isFromLeft = true; // is from left
int left = 1;
int remain = n;
int step = 1;
while(remain > 1) {
if(isFromLeft || remain % 2 == 1) {
left += step;
}
step = step << 1;
remain = remain >> 1;
isFromLeft = !isFromLeft;
}
return left;
}
};
