Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Partition Array Into Three Parts With Equal Sum in Python
Given an array of integers, we need to determine if we can partition it into three non-empty parts with equal sums. This means finding two partition points that divide the array into three contiguous subarrays where each subarray has the same sum.
Formally, we need to find indices i and j where i+1 < j such that:
- Sum of elements from index 0 to i equals
- Sum of elements from index i+1 to j-1 equals
- Sum of elements from index j to end
For example, with input [0,2,1,-6,6,-7,9,1,2,0,1], we can partition into [0,2,1], [-6,6,-7,9,1], and [2,0,1], each having sum 3.
Algorithm Steps
To solve this problem, we follow these steps −
- Calculate total sum and check if it's divisible by 3
- Create cumulative sum arrays from left and right
- Find valid partition points using two pointers
- Return true if valid partition exists
Example Implementation
Let us see the following implementation to get better understanding −
class Solution:
def canThreePartsEqualSum(self, A):
total_sum = sum(A)
if total_sum % 3 != 0:
return False
# Create cumulative sum arrays
sum_left = [0] * len(A)
sum_left[0] = A[0]
sum_right = [0] * len(A)
sum_right[-1] = A[-1]
# Fill left cumulative sum
for i in range(1, len(A)):
sum_left[i] = A[i] + sum_left[i-1]
# Fill right cumulative sum
for i in range(len(A)-2, -1, -1):
sum_right[i] = A[i] + sum_right[i+1]
required_sum = total_sum // 3
index1 = 0
index2 = len(A) - 1
# Find partition points
while index1 < index2:
while index1 < index2 and sum_left[index1] != required_sum:
index1 += 1
while index2 > index1 and sum_right[index2] != required_sum:
index2 -= 1
return index1 < index2
return False
# Test the solution
solution = Solution()
test_array = [0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1]
result = solution.canThreePartsEqualSum(test_array)
print(f"Can partition array: {result}")
# Test with another example
test_array2 = [0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 2]
result2 = solution.canThreePartsEqualSum(test_array2)
print(f"Can partition second array: {result2}")
Can partition array: True Can partition second array: False
How It Works
The algorithm works by:
- Validation: First checking if the total sum is divisible by 3
- Preprocessing: Creating left and right cumulative sum arrays
- Two Pointers: Using two indices to find valid partition points
- Verification: Ensuring the partition points create three non-empty parts
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(n) | Single pass to create cumulative arrays |
| Space | O(n) | Two additional arrays for cumulative sums |
Conclusion
This solution efficiently partitions an array into three equal-sum parts using cumulative sums and two pointers. The key insight is that if such a partition exists, each part must have sum equal to total_sum/3.
