-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitFlip.java
More file actions
44 lines (39 loc) · 1.13 KB
/
BitFlip.java
File metadata and controls
44 lines (39 loc) · 1.13 KB
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
public class BitFlip {
public static int bitFlip(int[] array, int length) {
int left = 0, right = length - 1;
int res = 0;
while (left <= right) {
while (array[left] == 1) {
left++;
}
while (array[right] == 1) {
right--;
}
int cf = countFlip(array, left, right);
res = res > cf ? res : cf;
while (array[left] == 0) {
left++;
}
while (array[right] == 0) {
right--;
}
}
return res;
}
private static int countFlip(int[] array, int left, int right) {
int count = 0;
for (int i = 0; i < left; i++) {
if (array[i] == 1) count++;
}
for (int i = left; i <= right; i++) {
if (array[i] == 0) count++;
}
for (int i = right + 1; i < array.length; i++) {
if (array[i] == 1) count++;
}
return count;
}
public static void main(String[] args) {
System.out.println(bitFlip(new int[] {1, 0, 0, 1, 0, 0, 1, 0}, 8));
}
}