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
Partition Array Into Three Equal Parts3120-39-244Input: [3, 1, 2, 0, -3, 9, -2, 4, 4]Partition 1Partition 2Part 1: Sum = 6Part 2: Sum = 6Part 3: Sum = 6[3, 1, 2][0, -3, 9][-2, 4, 4]All three parts have equal sum = 6Result: true
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
Asked in
Amazon 15 Google 8 Facebook 6
32.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen