From 9a505786660768c4f3b57bea348c1aec9c051714 Mon Sep 17 00:00:00 2001 From: srajansohani <99042007+srajansohani@users.noreply.github.com> Date: Sat, 15 Oct 2022 23:10:57 +0530 Subject: [PATCH] KadaneAlgorithm.cpp This Algorithm is used to find maximum subarray Sum in one iteration --- Arrays/c++/Kadane.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Arrays/c++/Kadane.cpp diff --git a/Arrays/c++/Kadane.cpp b/Arrays/c++/Kadane.cpp new file mode 100644 index 0000000..09d68b0 --- /dev/null +++ b/Arrays/c++/Kadane.cpp @@ -0,0 +1,17 @@ +#include +long long maxSubarraySum(int arr[], int n) +{ + long long int sum_till_now = 0; + //sum till now as soon as it does not get -ve we + long long int max_sum = 0; + for(int i = 0; i < n;i++){ + sum_till_now = sum_till_now + arr[i]; + if(sum_till_now > max_sum){ + max_sum = sum_till_now; + } + if(sum_till_now < 0){ + sum_till_now = 0; + } + } + return max_sum; +}