-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonkBinaryArray.java
125 lines (109 loc) · 2.76 KB
/
MonkBinaryArray.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* https://www.hackerearth.com/practice/basic-programming/bit-manipulation/basics-of-bit-manipulation/practice-problems/algorithm/monk-and-binary-array-1/
* #bit-manipulation
*/
/*
0 1 2 3 4 5 6 7 8 9
1 1 0 0 1 1 0 0 0 1
preSum 1 2 2 2 3 4 4 4 4 5
L R
---------------
-----
0 1 1 1 2 3 3 3 3 4
------------
[L, R] preSum[R] - preSum[L - 1] - > XOR = 1
0 1 2 3 4
oneList: 0 1 4 5 9
-> flip bit
0 1 2 3 4 5 6 7 8 9
1 1 0 0 1 1 0 0 0 1
|
*/
import java.util.Scanner;
class MonkBinaryArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sz = n + 1;
int[] arr = new int[sz];
int[] oneArr = new int[sz];
int val;
for (int i = 1; i < sz; i++) {
val = sc.nextInt();
arr[i] = val;
}
int ans = 0;
for (int i = 1; i < arr.length; i++) {
// attempt to flip bit
arr[i] ^= 1;
// calculate
int sum = 0;
// for (int j=1; j < arr.length; j++) {
// if (arr[j] == 1) {
// oneArr[j] = oneArr[j-1] + 1;
// } else {
// oneArr[j] = oneArr[j-1];
// }
// }
int numEven = 1;
int numOdd = 0;
int tot = 0;
for (int r = 1; r < oneArr.length; r++) {
if (arr[r] == 1) {
tot++;
}
if ((tot & 1) == 1) { // odd
sum += numEven;
numOdd++;
} else {
sum += numOdd;
numEven++;
}
}
if (ans < sum) {
ans = sum;
}
// reset
arr[i] ^= 1;
}
System.out.println(ans);
// Complexity:
// Space: O(N)
// Time: O(N^2)
// for (int i=1; i < arr.length; i++) {
// // attempt to flip bit
// arr[i] ^= 1;
// // calculate
// int sum = 0;
// for (int j=1; j < arr.length; j++) {
// if (arr[j] == 1) {
// oneArr[j] = oneArr[j-1] + 1;
// } else {
// oneArr[j] = oneArr[j-1];
// }
// }
// int numEven = 1;
// int numOdd = 0;
// for (int r = 1; r < oneArr.length; r++) {
// if (oneArr[r] & 1 == 1) { // odd
// sum += numEven;
// numOdd++;
// }
// else {
// sum += numOdd;
// numEven++;
// }
// // for (int l = 1; l <= r; l++) {
// // if ((oneArr[r] - oneArr[l - 1]) & 1 == 1) {
// // sum += 1;
// // }
// // }
// }
// if (ans < sum) {
// ans = sum;
// }
// // reset
// arr[i] ^= 1;
// }
}
}