From 8a22b6a0eb70eaf5b8da114c4fed6974392a2dd6 Mon Sep 17 00:00:00 2001 From: EdsonNeto08 Date: Fri, 16 Oct 2020 19:39:12 -0300 Subject: [PATCH] Adding Kadane's Algorithm in c++ --- kadane.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 kadane.cpp diff --git a/kadane.cpp b/kadane.cpp new file mode 100644 index 00000000..53e35e58 --- /dev/null +++ b/kadane.cpp @@ -0,0 +1,27 @@ +#include "bits/stdc++.h" +using namespace std; + +int max_subarray(vector &v){ + int max_current = v[0], max_global = v[0]; + for(int i = 1; i < v.size(); i++){ + max_current = max(max_current + v[i], v[i]); + max_global = max(max_current, max_global); + } + return max_global; +} + +int main(){ + int n; + vector v; + + cin >> n; + + for(int i = 0; i < n; i++){ + int a; + cin >> a; + v.push_back(a); + } + + cout << max_subarray(v) << '\n'; + +}