Skip to content

Commit b2e7a76

Browse files
authored
Add files via upload
1 parent ab7162a commit b2e7a76

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Maximum Subarray.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
5+
class Solution {
6+
public:
7+
int max;
8+
public int maxSubArray(int[] nums) {
9+
if (nums.length == 0) {
10+
return 0;
11+
}
12+
max = nums[0];
13+
maxSum(nums, 0);
14+
return max;
15+
}
16+
public int maxSum(int[] nums, int i) {
17+
if (i == nums.length) {
18+
return 0;
19+
}
20+
int res = Math.max(nums[i], nums[i] + maxSum(nums, i + 1));
21+
max = Math.max(max, res);
22+
return res;
23+
}
24+
};
25+
int main() {
26+
int n,i,j,ans;
27+
cout<<"No of elements";
28+
cin>>n;
29+
int nums[n];
30+
cout<<"Enter numbers";
31+
for(i=0;i<n;i++)
32+
{
33+
cin>>j;
34+
nums[i]=j;
35+
}
36+
Solution obj1;
37+
ans=obj1.maxSubArray(nums);
38+
cout<<ans;
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)