Problem Statement
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])
LeetCode
Constraints
3 <= arr.length <= 5 * 10^4
-10^4 <= arr[i] <= 10^4
Examples
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
Different Approaches
1️⃣ Brute Force Approach
Code:
class Solution {
public:
bool canThreePartsEqualSum(vector<int>& arr) {
int n = arr.size();
// Step 1: Calculate the total sum of the array
int totalSum = 0;
for (int num : arr) {
totalSum += num;
}
// Step 2: If total sum is not divisible by 3, we cannot split it into 3 equal parts
if (totalSum % 3 != 0) {
return false;
}
int targetSum = totalSum / 3;
// Step 3: Try all possible ways to split the array into 3 parts
// First cut can be at index i (left part: 0 to i)
for (int i = 0; i < n - 2; i++) {
int leftSum = 0;
// Calculate the sum from index 0 to i (first part)
for (int k = 0; k <= i; k++) {
leftSum += arr[k];
}
// If the first part doesn't sum to the target, skip
if (leftSum != targetSum) {
continue;
}
// Second cut can be at index j (middle part: i+1 to j)
for (int j = i + 1; j < n - 1; j++) {
int middleSum = 0;
// Calculate the sum from index i+1 to j (second part)
for (int k = i + 1; k <= j; k++) {
middleSum += arr[k];
}
// If the second part doesn't sum to the target, skip
if (middleSum != targetSum) {
continue;
}
// Step 4: The remaining part (j+1 to end) must also sum to the target
int rightSum = 0;
for (int k = j + 1; k < n; k++) {
rightSum += arr[k];
}
// If all three parts match the target sum, return true
if (rightSum == targetSum) {
return true;
}
}
}
// If no valid split found, return false
return false;
}
};
Complexity Analysis:
- Time Complexity:
O(n^3)
- Space Complexity:
O(1)
2️⃣ Prefix Sum Approach (Optimal Approach)
Approach:
- Compute totalSum.
If totalSum % 3 != 0 → return false. - Target sum for each part = totalSum / 3.
- Traverse array, keep currSum.
Count how many times currSum == target. - If you find 2 such partitions (i.e., 2 cuts), and you're not at the end, third part is valid.
🧠 Why This Works:
- Total sum divisible by 3 → each part must sum to targetSum.
- You scan from left to right, collecting sums.
- Every time you reach a targetSum, you reset the sum and increment the part count.
- Once you've found 2 valid parts, the rest of the array automatically becomes the 3rd part.
Code:
class Solution {
public:
bool canThreePartsEqualSum(vector<int>& arr) {
// Step 1: Calculate the total sum of the array
int totalSum = accumulate(arr.begin(), arr.end(), 0);
// Step 2: If total sum is not divisible by 3, we can't split it equally into 3 parts
if (totalSum % 3 != 0) return false;
int targetSum = totalSum / 3; // Each part must sum to this value
int currentSum = 0;
int partsFound = 0;
// Step 3: Traverse the array and count how many times we hit the target sum
for (int i = 0; i < arr.size(); ++i) {
currentSum += arr[i];
// Whenever we reach the target sum, we count it as one valid part
if (currentSum == targetSum) {
partsFound++; // Found one part
currentSum = 0; // Reset to start tracking the next part
// Step 4: If we already found 2 parts, and we're not at the end,
// the remaining elements automatically make the third part.
if (partsFound == 2 && i < arr.size() - 1) {
return true;
}
}
}
// Step 5: If we never found two valid parts, return false
return false;
}
};
Complexity Analysis:
- Time Complexity:
O(n)
- Space Complexity:
O(1)