Problem: Get maximum binary Gap.
For example, 9's binary form is 1001, the gap is 2.
http://www.fgdsb.com/2015/01/03/max-binary-gap/
ead full article from Twitter Codility Problem – Max Binary Gap
For example, 9's binary form is 1001, the gap is 2.
public class Solution { public static int solution(int N) { int max = 0; int count = -1; int r = 0; while (N > 0) { // get right most bit & shift right r = N & 1; N = N >> 1; if (0 == r && count >= 0) { count++; } if (1 == r) { max = count > max ? count : max; count = 0; } } return max; } public static void main(String[] args) { System.out.println(solution(9)); } } |
ead full article from Twitter Codility Problem – Max Binary Gap