-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryGap.java
More file actions
46 lines (38 loc) · 1022 Bytes
/
BinaryGap.java
File metadata and controls
46 lines (38 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package codility;
/*
* Score 100%
*
* https://codility.com/programmers/task/binary_gap/
*/
public class BinaryGap {
public int solution(int N) {
int n=N;
int zeroCount =0;
int longestRun=0;
boolean encounteredOne = false;
do {
int bit = n %2;
if( bit == 0 ){
zeroCount++;
}
else {
if( encounteredOne ) {
longestRun = Math.max(longestRun, zeroCount);
}
encounteredOne =true;
zeroCount = 0;
}
n = n / 2;
} while( n !=0);
return longestRun;
}
public static void main(String args[]) {
BinaryGap instance = new BinaryGap();
System.err.println( instance.solution(9) );
System.err.println( instance.solution(529) );
System.err.println( instance.solution(20) );
System.err.println( instance.solution(15) );
System.err.println( instance.solution(1041) );
System.err.println( instance.solution(2147483647));
}
}