-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnnsa.cpp
More file actions
48 lines (46 loc) · 731 Bytes
/
mnnsa.cpp
File metadata and controls
48 lines (46 loc) · 731 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
#include<iostream>
#include<vector>
using namespace std;
vector<int> maxset(vector<int>& A) {
int l = 0, r = 0, maxi = INT_MIN, sum = 0;
int s = 0, e = 0;
vector<int> v;
for (int i = 0; i < A.size(); i++)
{
sum += A[i];
if (sum < 0)
{
l = r = i;
sum = 0;
}
else {
r = i;
}
if (sum > maxi)
{
s = l; e = r;
maxi = sum;
}
}
cout << "Maxi: " << maxi << endl;
if (maxi == 0) return vector<int>();
v.resize((e - s + 1), 0);
int j = 0;
for (int i = s; i <= e; i++)
{
v[j] = A[i];
j++;
}
return v;
}
int main()
{
vector<int> A = { 0,0,-1,0 };
vector<int> ans = maxset(A);
for (auto a = ans.begin(); a != ans.end(); a++)
{
cout << *a << " ";
}
cout << endl;
return 0;
}