Skip to content

Commit 22acda9

Browse files
authored
Create main.cpp
1 parent 19f783a commit 22acda9

File tree

1 file changed

+16
-0
lines changed
  • 05 - Arrays Based Problems/Maximum Subarray

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int maxSubArray(vector<int>& nums) {
4+
int sum = 0;
5+
int maxi = nums[0];
6+
7+
for(int i = 0; i < nums.size(); i++){
8+
sum += nums[i];
9+
maxi = max(maxi, sum);
10+
11+
if(sum < 0) sum = 0;
12+
}
13+
14+
return maxi;
15+
}
16+
};

0 commit comments

Comments
 (0)