Skip to content

Commit 8fe2bb1

Browse files
committed
added a problem
1 parent 62d127c commit 8fe2bb1

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
2+
3+
For example:
4+
5+
Given the array [-2,1,-3,4,-1,2,1,-5,4],
6+
7+
the contiguous subarray [4,-1,2,1] has the largest sum = 6.
8+
9+
For this problem, return the maximum sum.

Max_Sum_Contiguous_Subarray/Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A good tricky problem.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
int Solution::maxSubArray(const vector<int> &A) {
2+
int s=INT_MIN,n = A.size(),i,j,m=0,mi=INT_MAX,ma = INT_MIN;
3+
int arr[n];
4+
memset(arr,0,sizeof(arr));
5+
6+
for(i=0;i<n;i++)
7+
{
8+
m+=A[i];
9+
arr[i] = m;
10+
}
11+
if(n==1)
12+
return A[0];
13+
s=arr[0];
14+
for(i=0;i<n-1;i++)
15+
{
16+
mi = min(mi,arr[i]);
17+
s = max(s,arr[i+1]-mi);
18+
}
19+
return s;
20+
}

0 commit comments

Comments
 (0)