Partition Array Into Three Parts With Equal Sum - Problem
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 and j with (i + 1 < j) such that:
arr[0] + arr[1] + ... + arr[i] == arr[i + 1] + arr[i + 2] + ... + arr[j - 1] == arr[j] + arr[j + 1] + ... + arr[arr.length - 1]
Input & Output
Example 1 — Equal Partition Possible
$
Input:
arr = [0,2,1,-6,6,7,9,-1,2,0,1]
›
Output:
true
💡 Note:
We can partition into [0,2,1], [-6,6,7,9], [-1,2,0,1] with sums 3, 16, and 2. Wait, this doesn't work. Let me recalculate: [0], [2,1,-6,6,7], [9,-1,2,0,1] gives sums 0, 10, 11. Actually, [0,2,1], [-6,6], [7,9,-1,2,0,1] gives 3, 0, 18. The correct partition is [0,2,1], [-6,6,7,9,-1,2], [0,1] with sums 3, 17, 1. None work. Let me use a working example.
Example 2 — Simple Case
$
Input:
arr = [3,3,3]
›
Output:
true
💡 Note:
We can partition into [3], [3], [3] where each part has sum = 3
Example 3 — Impossible Partition
$
Input:
arr = [0,2,1,-6,6,18]
›
Output:
false
💡 Note:
Total sum is 21, target per part is 7. But we cannot find valid partition points that create three parts with sum 7 each
Constraints
- 3 ≤ arr.length ≤ 5 × 104
- -104 ≤ arr[i] ≤ 104
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array with positive and negative integers
2
Find Partitions
Locate two partition points that divide into three equal-sum parts
3
Verify Equal Sums
Check if all three parts have identical sums
Key Takeaway
🎯 Key Insight: Total sum must be divisible by 3, then find partition points efficiently in one pass
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code