-
Notifications
You must be signed in to change notification settings - Fork 1
/
Question53.java
41 lines (29 loc) · 927 Bytes
/
Question53.java
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
package cracking.the.code.interview.chapter5;
/**
* You have an integer, and you can flip exactly one bit from 0 to 1.
* Write code to find the length of the longest sequence of 1's you could create.
*/
public class Question53 {
public static int findLongestSequenceOf1s(long n) {
int current = 0;
int previous = 0;
int max = 1;
while (n != 0) {
if ((n & 1) != 0) {
current++;
} else {
if ((n & 2) != 0) { // next bit is one
previous = current;
} else { // next bit is zero
previous = 0;
}
current = 0;
}
if (max < (current + previous + 1)) {
max = current + previous + 1;
}
n = n >>> 1L;
}
return max;
}
}