class Solution {
public:
int lastRemaining(int n) {
int start = 1, step = 1;
while (n > 1) {
start += step + (n-2)/2 * 2*step;
n /= 2;
step *= -2;
}
return start;
}
};
#include <iostream>
using namespace std;
class Solution {
public:
int lastRemaining(int n) {
if(n == 1)
return 1;
return 2 * (1 + n/2 - lastRemaining(n/2));
}
};
int main() {
cout << Solution().lastRemaining(9) << endl;
return 0;
}