-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkadane_demo.cpp
71 lines (62 loc) · 1.91 KB
/
kadane_demo.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
wiki: https://en.wikipedia.org/wiki/Maximum_subarray_problem
geeksforgeeks: http://www.geeksforgeeks.org/largest-sum-contiguous-subarray/
*/
#include <iostream>
#include <algorithm>
#include <vector>
/*
Kadane's algorithm: https://en.wikipedia.org/wiki/Maximum_subarray_problem as it is
returns max sum
*/
int kadane(std::vector<int> A) {
// initialize max variables with first element
int current_sum = A[0];
int max_sum = current_sum;
for (int i = 1; i < (int) A.size(); i++) {
current_sum = std::max(A[i], current_sum + A[i]);
max_sum = std::max(max_sum, current_sum);
}
return max_sum;
}
/*
What if we need starting and ending index we used to compute the max sum
- general-solving/lintcode/402_continuous-subarray-sum.cpp
used in solving the lintcode problem
*/
std::vector<int> kadane_with_index(std::vector<int> A) {
// initialize max variables with first element
int current_sum = A[0];
int max_sum = current_sum;
int min_index = 0;
int final_min_index = 0;
int max_index = 0;
for (int i = 1; i < (int)A.size(); i++) {
// continue with pevious sequence
if (A[i] < current_sum + A[i]) {
// if (current_sum > 0) { // equivalent to previous condition
current_sum += A[i];
}
else
// we should restart the sequence from current index
{
// if I add previous sum it will not maximize sum
current_sum = A[i];
min_index = i;
}
if (max_sum < current_sum) {
max_sum = current_sum;
final_min_index = min_index;
max_index = i;
}
}
return{ final_min_index, max_index };
}
int main() {
// Testcase 1: {-7, 3, 4, -10, 5, 6}
int sum = kadane({ { -7, 1, 2, -10, 2, 3 } });
std::cout << "Max sum: " << sum << std::endl;
std::vector<int> result = kadane_with_index({ 1, -7, -1, -2 });
std::cout << "Maximum subarray interval: [" << result[0] << ", " << result[1] << "]" << std::endl;
return 0;
}