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<