-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCTDL027.cpp
51 lines (49 loc) · 963 Bytes
/
SCTDL027.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
/**
* @file SCTDL027.cpp
* @author your name ([email protected])
* @brief program to print largest contiguous array sum
* @version 0.1
* @date 2023-03-13
*
* @copyright Copyright (c) 2023
*
*/
#include <bits/stdc++.h>
using namespace std;
int maxSubArraySum(vector<int> vt, int size)
{
int max_so_far = INT_MIN, max_ending_here = 0;
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + vt[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
}
}
return max_so_far;
}
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
vector<int> vt;
for(int i = 0; i < n; i++)
{
int tmp;
cin >> tmp;
vt.push_back(tmp);
}
int max_sum = maxSubArraySum(vt, n);
cout << max_sum << endl;
}
return 0;
}