andrzej-c
7/22/2016 - 7:49 PM

Binary gap

Binary gap

// you can also use imports, for example:
// import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");

class Solution {
    public int solution(int N) {
        // write your code in Java SE 8
        int result = 0;
        int tmp = 0;
        boolean used = false;
        while(N!=0){
            int sign=N%2;
            N=N/2;
            if(sign==1){
                used = true;
                if(tmp>result){
                    result = tmp;
                    tmp=0;
                }
            } else if (used && sign==0){
                tmp++;
            }
        }
        return result;
    }
}