Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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] <= 104Problem summary: 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])
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[0,2,1,-6,6,-7,9,1,2,0,1]
[0,2,1,-6,6,7,9,-1,2,0,1]
[3,3,6,5,-2,2,5,1,-9,4]
find-the-middle-index-in-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1013: Partition Array Into Three Parts With Equal Sum
class Solution {
public boolean canThreePartsEqualSum(int[] arr) {
int s = Arrays.stream(arr).sum();
if (s % 3 != 0) {
return false;
}
s /= 3;
int cnt = 0, t = 0;
for (int x : arr) {
t += x;
if (t == s) {
cnt++;
t = 0;
}
}
return cnt >= 3;
}
}
// Accepted solution for LeetCode #1013: Partition Array Into Three Parts With Equal Sum
func canThreePartsEqualSum(arr []int) bool {
s := 0
for _, x := range arr {
s += x
}
if s%3 != 0 {
return false
}
s /= 3
cnt, t := 0, 0
for _, x := range arr {
t += x
if t == s {
cnt++
t = 0
}
}
return cnt >= 3
}
# Accepted solution for LeetCode #1013: Partition Array Into Three Parts With Equal Sum
class Solution:
def canThreePartsEqualSum(self, arr: List[int]) -> bool:
s, mod = divmod(sum(arr), 3)
if mod:
return False
cnt = t = 0
for x in arr:
t += x
if t == s:
cnt += 1
t = 0
return cnt >= 3
// Accepted solution for LeetCode #1013: Partition Array Into Three Parts With Equal Sum
impl Solution {
pub fn can_three_parts_equal_sum(arr: Vec<i32>) -> bool {
let sum: i32 = arr.iter().sum();
let s = sum / 3;
let mod_val = sum % 3;
if mod_val != 0 {
return false;
}
let mut cnt = 0;
let mut t = 0;
for &x in &arr {
t += x;
if t == s {
cnt += 1;
t = 0;
}
}
cnt >= 3
}
}
// Accepted solution for LeetCode #1013: Partition Array Into Three Parts With Equal Sum
function canThreePartsEqualSum(arr: number[]): boolean {
let s = arr.reduce((a, b) => a + b);
if (s % 3) {
return false;
}
s = (s / 3) | 0;
let [cnt, t] = [0, 0];
for (const x of arr) {
t += x;
if (t == s) {
cnt++;
t = 0;
}
}
return cnt >= 3;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.