From 63a4afefc5334eb226c6ecc164b67cedb626fe12 Mon Sep 17 00:00:00 2001 From: Ayushi C <67193440+AyushiChakrabarty@users.noreply.github.com> Date: Wed, 2 Sep 2020 13:01:56 +0530 Subject: [PATCH] Create Kadane_Maximum sum subarray --- Kadane_Maximum sum subarray | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Kadane_Maximum sum subarray diff --git a/Kadane_Maximum sum subarray b/Kadane_Maximum sum subarray new file mode 100644 index 0000000..c74c5e0 --- /dev/null +++ b/Kadane_Maximum sum subarray @@ -0,0 +1,33 @@ +//Maximum Sum Subarray_O(n) +#include +#include +#include +using namespace std; + +int mss(int arr[],int n) +{ + int i; + int ans = arr[0],sum = 0; + + for(i= 1;i < n; ++i) + ans = max(ans,arr[i]); + if(ans<0) + return ans; + ans = 0; + for(i = 0 ;i < n; ++i) + { + if(sum + arr[i] > 0) + sum+=arr[i]; + else + sum = 0; + ans = max(ans,sum); + } + return ans; +} + +int main(int argc, char const *argv[]) +{ + int arr[] = {3,-2,5,-1}; + cout<