-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path28 NOV 28 November Maximum Sub Array
56 lines (43 loc) · 1.09 KB
/
28 NOV 28 November Maximum Sub Array
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
class Solution{
public:
vector<int> findSubarray(int a[], int n) {
int s=0,e=0, ms=-1, me=-1,l=0;
long long sum = 0, maxs = 0;
for(int i=0;i<n;i++)
{
if(a[i] < 0){
sum=0;
e = i+1;
s = i+1;
}
else
{
sum+=a[i];
if(sum >= maxs){
//ms = s;
//me = i;
//maxs = sum;
if(sum>maxs)
{
ms = s;
me = i;
}
else
{
if(i-s+1 > l)
ms = s;
me = i;
}
maxs = sum;
l=me-ms+1;
}
}
}
if(ms == -1 and me == -1) return {-1};
vector<int> res;
for(int i=ms;i<=me;i++){
res.push_back(a[i]);
}
return res;
}
};