-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSS_DP.cpp
40 lines (35 loc) · 948 Bytes
/
MSS_DP.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
/*def mss_bottom_up(A[1...n])
max_suf is array [1...n]
max_suf[1] = A[1]
for i from 2 to n
max_suf[i] = max(max_suf[i-1]+A[i],A[i])
mss A[i]
for i from 2 to n
mss = max(mss,max(A[i],max_suf[i-1]))
return mss
end*/
// C++ program to print largest contiguous array sum
#include <bits/stdc++.h>
using namespace std;
int maxSubArraySum(int a[], 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 + a[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;
}
// Driver Code
int main()
{
int a[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
int n = sizeof(a) / sizeof(a[0]);
// Function Call
int max_sum = maxSubArraySum(a, n);
cout << "Maximum contiguous sum is " << max_sum;
return 0;
}