[AlgoExpert] Kadane`s Algorithm

Kadane’s Algorithm

  • Time : O(n)
  • Space : O(1)
1
2
3
4
5
6
7
8
int kadanesAlgorithm(vector<int> array) {
int res = array[0];
for(int i = 0, sum = 0; i < array.size(); i++) {
sum = max(sum + array[i], array[i]);
res = max(sum, res);
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/16/PS/AlgoExpert/kadane-algorithm/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.