1013. Partition Array Into Three Parts With Equal Sum

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
class Solution {
public boolean canThreePartsEqualSum(int[] arr) {
int totalSum = 0;
for (int num : arr) {
totalSum += num;
}

if (totalSum % 3 != 0) {
return false;
}

int targetSum = totalSum / 3;
int count = 0;
int sum = 0;
for (int num : arr) {
sum += num;
if (sum == targetSum) {
sum = 0;
count++;
}
}

return count >= 3;
}
}

References

1013. Partition Array Into Three Parts With Equal Sum