-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaximumSubsequenceSum.cpp
83 lines (67 loc) · 1.74 KB
/
MaximumSubsequenceSum.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
*
* Title: Maximum Sum of Non-Empty Subsequence
* Description: Display the maximum sum of any non-empty subsequence of a given list of integers.
* Author: Ajit Panigrahi
* GitHub: https://github.com/AjitZero
* Twitter: https://twitter.com/AjitZero
*
*/
#include <iostream>
#include <vector>
using namespace std;
/**
* Calculates the maximum sum of all possible subsequences
* @param a [Input integer list]
* @param noOfElements [Size of list]
* @return [maximum sum result]
*/
int MaximumSubsequenceSum(vector<int> const &a, const int noOfElements) {
/**
* If at least one non-negative number exists,
* the maximum subsequence sum is the sum of all non-negative integers
*/
int totalSum = 0;
/**
* If no non-negative numbers exist,
* the maximum subsequence sum is the largest negative number in the list
*/
int largestNumber = a[0];
// Iterating through the list
for (int i = 0; i < noOfElements; ++i) {
// Calculating largest number for every new element
if (a[i] > largestNumber) {
largestNumber = a[i];
}
// Calculating sum of all positive numbers
if (a[i] > 0) {
totalSum += a[i];
}
}
// Returns the sum of all positive integers or zero, if at least one non-negative integer exists
if (largestNumber > 0) {
return totalSum;
}
// Returns the largest negative number, if at least no non-negative integer exists
return largestNumber;
}
// Driver function
int main() {
int noOfElements;
cin >> noOfElements;
vector<int> a(noOfElements);
for (int i = 0; i < noOfElements; ++i) {
cin >> a[i];
}
cout << MaximumSubsequenceSum(a, noOfElements);
return 0;
}
/*
Sample Input:
--------------------
7
20 -17 34 11 -5 20 -5 2
Sample Output:
--------------------
85
*/