How do you find the maximum sum of a Subarray?
How do you find the maximum subarray of an array?
maximum subarray sum with positive and negative number(O(n^3))
#include<bits/stdc++.h>
using namespace std;
int main()
{
int ans=0,sum; //because empty subarray sum is zero
int i,j,k,n;
cin>>n;
int a[n];
for(i=0;i<n;i++) cin>>a[i];
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
sum=0;
for(k=i;k<=j;k++)
{
sum+=a[k];
}
ans=max(ans,sum);
}
}
cout<<ans<<endl;
return 0;
}
0 Comments