We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ab7162a commit b2e7a76Copy full SHA for b2e7a76
Maximum Subarray.cpp
@@ -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
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
41
0 commit comments