Elimination Game

public class Solution {
    public int lastRemaining(int n) {
        boolean left = true;
        int remain = n;
        int head = 1;
        int step = 1;
        while(remain>1){
            if(left || remain%2==1){
                head += step;
            }
            step *= 2;
            remain /= 2;
            left = !left;
        }
        return head;
    }
}
comments powered by Disqus