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] where each part has sum = 5. Actually, let me use a clearer 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code