forked from sonumahajan/All_Program_helper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxsubsequence.cpp
More file actions
45 lines (35 loc) · 717 Bytes
/
maxsubsequence.cpp
File metadata and controls
45 lines (35 loc) · 717 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to print the maximum
// non-emepty subsequence sum
int MaxNonEmpSubSeq(int a[], int n)
{
// Stores the maximum non-emepty
// subsequence sum in an array
int sum = 0;
// Stores the largest element
// in the array
int max = *max_element(a, a + n);
if (max <= 0) {
return max;
}
// Traverse the array
for (int i = 0; i < n; i++) {
// If a[i] is greater than 0
if (a[i] > 0) {
// Update sum
sum += a[i];
}
}
return sum;
}
// Driver Code
int main()
{
int arr[] = { -2, 11, -4, 2, -3, -10 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << MaxNonEmpSubSeq(arr, N);
return 0;
}