Skip to content

Commit 575e80f

Browse files
authored
Create maximum-split-of-positive-even-integers.cpp
1 parent 29dd55b commit 575e80f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(sqrt(n))
2+
// Space: O(1)
3+
4+
// greedy
5+
class Solution {
6+
public:
7+
vector<long long> maximumEvenSplit(long long finalSum) {
8+
if (finalSum % 2) {
9+
return {};
10+
}
11+
vector<long long> result;
12+
for (int i = 2; i <= finalSum; i += 2) {
13+
result.emplace_back(i);
14+
finalSum -= i;
15+
}
16+
result.back() += finalSum;
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)