Skip to content

Commit 38bd1b4

Browse files
authored
Create partition-array-into-three-parts-with-equal-sum.cpp
1 parent 1a06639 commit 38bd1b4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
bool canThreePartsEqualSum(vector<int>& A) {
7+
const auto& total = accumulate(A.cbegin(), A.cend(), 0);
8+
if (total % 3 != 0) {
9+
return false;
10+
}
11+
int curr = 0, parts = 0;
12+
for (const auto& x : A) {
13+
curr += x;
14+
if (sum == total / 3) {
15+
++parts, curr = 0;
16+
}
17+
}
18+
return parts >= 3;
19+
}
20+
};

0 commit comments

Comments
 (0)