-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySequence.cpp
More file actions
55 lines (43 loc) · 870 Bytes
/
Copy pathbinarySequence.cpp
File metadata and controls
55 lines (43 loc) · 870 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
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <algorithm>
using namespace std;
int findPosition(int arr[], int size) {
int numofZero = 0;
int numofOne = 0;
for (int i=0; i<size; ++i) {
if (arr[i] == 0) {
++numofZero;
}
else if (arr[i] == 1) {
++numofOne;
}
}
int l0, l1, r0, r1, pos, curMin, minError;
l0 = l1 = pos = 0;
r0 = numofZero - l0;
r1 = numofOne - l1;
minError = size*10;
for (int i=0; i<size; ++i) {
if (arr[i] == 1) {
++l1;
--r1;
}
else if (arr[i] == 0) {
++l0;
--r0;
}
curMin = min(l1+r0, l0+r1);
minError = min(minError, curMin);
if (minError == curMin) {
pos = i;
};
}
cout<<"pos-"<<pos<<endl;
cout<<"min-"<<minError<<endl;
return 0;
}
int main() {
int arr[] = {0,0,0,1,1,0,1,1};
findPosition(arr, sizeof(arr)/sizeof(int));
return 0;
}