-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkadane_algo.cpp
48 lines (40 loc) · 881 Bytes
/
kadane_algo.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
// #include <iostream>
// #include <vector>
// using namespace std;
// int main()
// {
// int n = 5;
// int arr[5] = {1, 2, 3, 4, 5};
// for (int st = 0; st < n; st++)
// {
// for (int end = st; end < n; end++)
// {
// for (int i = st; i <= end; i++)
// {
// cout << arr[i];
// }
// cout << " ";
// }
// cout << endl;
// }
// }
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 5;
int arr[5] = {1, 2, 3, 4, 5};
int maxSum = INT8_MIN;
for (int st = 0; st < n; st++)
{
int currSum = 0;
for (int end = st; end < n; end++)
{
currSum += arr[end];
maxSum = max(currSum, maxSum);
}
}
cout << "max subarray sum = " << maxSum << endl;
return 0;
}