Golang version of the easiest solution


  • 0
    R
    func lastRemaining(n int) int {
        left := true
        remaining := n
        step := 1
        head := 1
            for remaining > 1 {
                if left || remaining % 2 == 1 {
                    head = head + step
                }
                remaining /= 2
                step = step * 2
                left = !left
            }
            return head
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.