Elimination Game LeetCode solution in C++

1
class Solution {
2
public:
3
int lastRemaining(int n) {
4
bool left = true;
5
int head = 1, step = 1; // step is the difference between adjacent elements.
6
while (n > 1) {
7
if (left || (n & 1)) { //(n&1)->odd
8
head = head + step;
9
}
10
step = step * 2;
11
n = n / 2;
12
left = !left;
13
}
14
return head;
15
}
16
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0