Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include<cmath>
#include<iostream>
#include<climits>
using namespace std;
int Maximum_Sum_Subarray(int arr[],int n) //Overall Time Complexity O(n)
{
int ans = A[0],sum = 0;
for(int i = 1;i < n; ++i) //Check if all are negative
ans = max(ans,arr[i]);
if(ans<0) //if Max < 0 return Max
return ans;
ans = 0;
for(int 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<<MSS(arr,4)<<"\n";
return 0;
}
@RoelCastano

This comment has been minimized.

Copy link

RoelCastano commented Sep 20, 2015

Last change does not compile.
A is not declared.
Thanks for the tutorials!

@atharvajakkanwar

This comment has been minimized.

Copy link

atharvajakkanwar commented May 27, 2017

int ans = A[0],sum = 0;

int ans = arr[0], sum = 0; ✔️

@kunaltyagi9

This comment has been minimized.

Copy link

kunaltyagi9 commented Nov 2, 2018

please start making videos again on your Youtube Channel. It's a request !

@Himanshupal0001

This comment has been minimized.

Copy link

Himanshupal0001 commented Jul 2, 2019

please start making videos again on your Youtube Channel. It's a request!

Bruh He is no more.

@agrawalsajal02

This comment has been minimized.

Copy link

agrawalsajal02 commented Jul 3, 2019

thanks for the code

@Aditya28061999

This comment has been minimized.

Copy link

Aditya28061999 commented Sep 26, 2019

please start making videos again on your Youtube Channel. It's a request !

bro, the owner of mycodeschool has been died in road accident...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.