-
Notifications
You must be signed in to change notification settings - Fork 160
/
Longest subarray with equal sum.cpp
88 lines (61 loc) · 1.81 KB
/
Longest subarray with equal sum.cpp
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
/*
Given 2 binary arrays A and B i.e. containing only 0s and 1s each of size N. Find indices i,j
such that Sum of elements from i to j in both arrays is equal and j-i (i.e. the length
of the set i,j) is the maximum possible value.
*/
/*
solution:
Suppose we have two Arrays:
A=[ 0 1 0 0 0 1 0 0 1 0 0]
B=[ 1 0 1 1 1 0 1 1 0 1 0]
Then we create two arrays : C[i] = A[i] - B[i] . D[i] = D[i-1] + C[i];
C=[-1 1 -1 -1 -1 1 -1 -1 1 -1 0]
D=[-1,0,-1,-2,-3,-2,-3,-4,-3,-4,-4]
now we consider D-array. if D[i] == D[j] , it means sum( C[i,j ] ) ==0 .
and since C[i] =A[i] - B[i] , now it means sum( A[i,j] ) == sum ( B[i, j] )
so now the task becomes find two indices i, j in D[], such that D[i]=D[j] and (j - i) is maximum.
we can use a map to solve this new problem
O(n) time, O(n) space
*/
#include<map>
#include<vector>
#include<cassert>
#include<iostream>
using namespace std;
int LongSubarrayEq(const vector<int>& a,const vector<int>& b) {
int na = a.size(), nb = b.size();
assert(na == nb);
vector<int> c(na,0);
for (int i = 0; i < na; ++i) {
c[i] = a[i] - b[i];
}
for (int i = 1; i < na; ++i) {
c[i] = c[i-1] + c[i];
}
int result = -1;
map<int,int> exist; //(c[i], i)
map<int,int>::iterator it;
exist.insert(pair<int,int>(0,-1));
for (int i = 0; i < na; ++i) {
it = exist.find(c[i]);
if ( it == exist.end()) { // not exist
exist.insert(it, pair<int,int>(c[i],i));
} else {
result = max(result, (i - it->second) );
}
}
return result;
}
int main() {
int n = 11;
vector<int> a(n,0);
vector<int> b(n,0);
a[1] = 1, a[5] = 1, a[8] = 1;
b[0] = 1, b[2] = 1, b[3] = 1, b[4] = 1,b[6] = 1, b[7] = 1, b[9] = 1;
cout<<LongSubarrayEq(a,b)<<endl;
/*
int a[]={0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0};
int b[]={1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0};
*/
return 0;
}