## Algorithm

Problem Name: 1013. Partition Array Into Three Parts With Equal Sum

Given an array of integers `arr`, return `true` if we can partition the array into three non-empty parts with equal sums.

Formally, we can partition the array if we can find indexes `i + 1 < j` with `(arr[0] + arr[1] + ... + arr[i] == arr[i + 1] + arr[i + 2] + ... + arr[j - 1] == arr[j] + arr[j + 1] + ... + arr[arr.length - 1])`

Example 1:

```Input: arr = [0,2,1,-6,6,-7,9,1,2,0,1]
Output: true
Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
```

Example 2:

```Input: arr = [0,2,1,-6,6,7,9,-1,2,0,1]
Output: false
```

Example 3:

```Input: arr = [3,3,6,5,-2,2,5,1,-9,4]
Output: true
Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
```

Constraints:

• `3 <= arr.length <= 5 * 104`
• `-104 <= arr[i] <= 104`

## Code Examples

### #1 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public boolean canThreePartsEqualSum(int[] A) {
int totalSum = 0;
for (int num : A) {
totalSum += num;
}
if (totalSum % 3 != 0) {
return false;
}
totalSum = totalSum / 3;
int currSum = 0;
int segmentCount = 0;
for (int num : A) {
currSum += num;
if (currSum == totalSum) {
segmentCount++;
currSum = 0;
}
}
return segmentCount >= 3;
}
}
``````
Copy The Code &

Input

cmd
arr = [0,2,1,-6,6,-7,9,1,2,0,1]

Output

cmd
true

### #2 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const canThreePartsEqualSum = function(A) {
let lo = 0
let hi = A.length - 1
let lsum = 0
let hsum = 0
const sum = A.reduce((ac, el) => ac + el, 0)
if(sum % 3 !== 0) return false
const target = sum / 3

while(lo < hi && lsum !== target) {
lsum += A[lo]
lo++
}
if(lsum !== target) return false
while(lo < hi && hsum !== target) {
hsum += A[hi]
hi--
}
if(hsum !== target) return false

let msum = 0
for(let i = lo; i <= hi; i++) {
msum += A[i]
}
if(msum !== target) return false
return true
};
``````
Copy The Code &

Input

cmd
arr = [0,2,1,-6,6,-7,9,1,2,0,1]

Output

cmd
true

### #3 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def canThreePartsEqualSum(self, A: List[int]) -> bool:
tar = sum(A) // 3
sm = cnt = 0
for a in A:
sm += a
if sm == tar:
sm = 0
cnt += 1
return cnt == 3
``````
Copy The Code &

Input

cmd
arr = [0,2,1,-6,6,7,9,-1,2,0,1]

Output

cmd
false

### #4 Code Example with C# Programming

```Code - C# Programming```

``````
namespace LeetCode
{
public class _1013_PartitionArrayIntoThreePartsWithEqualSum
{
public bool CanThreePartsEqualSum(int[] A)
{
var sum = 0;
foreach (var num in A)
sum += num;

if (sum % 3 != 0) return false;

var target = sum / 3;
var count = 0;
var currentSum = 0;
foreach (var num in A)
{
currentSum += num;
if (currentSum == target)
{
count++;
currentSum = 0;
}
}

return count > 2;
}
}
}
``````
Copy The Code &

Input

cmd
arr = [0,2,1,-6,6,7,9,-1,2,0,1]

Output

cmd
false