diff --git a/16. Moderate problems/16.17. 2016_max_subarray.cpp b/16. Moderate problems/16.17. 2016_max_subarray.cpp new file mode 100644 index 0000000..bc46591 --- /dev/null +++ b/16. Moderate problems/16.17. 2016_max_subarray.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +using namespace std; + +int maxSubArraySum(vector a, int size) +{ + int max_so_far = 0, max_ending_here = 0; + for(int i = 0; i> testcases; + while(testcases--){ + cin >> n; + vector x(n); + int cont = 0, noncont = 0, negmaxpos, negmax = 0; + for(size_t i = 0; i> x[i]; + if(x[i] > 0) noncont += x[i]; + if(x[i] < negmax){ + negmaxpos = i; + negmax = x[i]; + } + } + if(n == 1){ + cont = x[0]; noncont = cont; + }else if(negmax == 0) cont = noncont; // all positive + else if(noncont == 0){ // all negative + sort(x.begin(),x.end()); + cont = x[n-1]; noncont = cont; + }else cont = maxSubArraySum(x,n); + cout << cont << " " << noncont << endl; + } + return 0; +}