Given an array of integers arr
, determine if it can be partitioned into three non-empty parts with equal sums.
Formally, you must find indices i + 1 < j
such that the following is true:
(arr[0] + arr[1] + ... + arr[i] == arr[i + 1] + arr[i + 2] + ... + arr[j - 1] == arr[j] + arr[j + 1] + ... + arr[arr.length - 1])
Here are some examples to illustrate the problem:
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 = 3
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 = 6
Your task is to write a function that takes an array of integers as input and returns true
if the array can be partitioned as described, and false
otherwise.
What is the most efficient way to solve this problem?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method to split something into three equal parts involves systematically trying every possible combination. It's like exploring every single cut you could make until you find a way to divide it equally. This approach leaves no stone unturned but can be tedious.
Here's how the algorithm would work step-by-step:
def can_three_parts_equal_sum(arr):
array_sum = sum(arr)
# If total sum is not divisible by 3, it cannot be equally partitioned
if array_sum % 3 != 0:
return False
target_sum = array_sum / 3
array_length = len(arr)
for first_partition_end in range(array_length - 2):
first_partition_sum = sum(arr[0:first_partition_end + 1])
# Check if the first partition equals target sum
if first_partition_sum == target_sum:
for second_partition_end in range(first_partition_end + 1, array_length - 1):
second_partition_sum = sum(arr[first_partition_end + 1:second_partition_end + 1])
# Check if the second partition equals target sum
if second_partition_sum == target_sum:
third_partition_sum = sum(arr[second_partition_end + 1:])
#Checking all partitions are equal to the target.
if third_partition_sum == target_sum:
return True
# No valid partitions found
return False
The core idea is to figure out what the target sum for each of the three parts should be and then try to find those parts. We achieve efficiency by summing up the values one by one, stopping as soon as a part adds up to the target sum.
Here's how the algorithm would work step-by-step:
def can_three_parts_equal_sum(array_of_numbers) -> bool:
total_sum = sum(array_of_numbers)
\
if total_sum % 3 != 0:
return False
\
target_sum = total_sum // 3
current_sum = 0
parts_found = 0
\
for number in array_of_numbers:
current_sum += number
# Check if current partition sum matches the target
if current_sum == target_sum:
parts_found += 1
current_sum = 0
\
# Need to find exactly three parts that match target
return parts_found >= 3
Case | How to Handle |
---|---|
Null or empty input array | Return false immediately as partitioning is impossible. |
Array with fewer than 3 elements | Return false because partitioning into three non-empty parts is impossible. |
Array sum is not divisible by 3 | Return false since equal partitioning is not possible. |
Array contains only zeros | The solution should correctly identify two partition points to create three equal zero-sum parts. |
Array contains very large positive or negative numbers leading to potential integer overflow during sum calculation | Use long data type for sum calculations to prevent integer overflow. |
Array where one part is extremely large compared to the others | Iterate through the array to find two valid partitions which are not necessarily next to each other and may have different sizes, but same sum. |
Multiple valid partition points exist; identify any valid solution | Return true as soon as the first valid partition is found; no need to find all possibilities. |
Array contains duplicates with negative numbers resulting in the target sum | Iterate through the array until two valid partition points are found considering all negative and duplicate number combinations. |