Similar Problems
Similar Problems not available
Partition Array Into Three Parts With Equal Sum - Leetcode Solution
Companies:
LeetCode: Partition Array Into Three Parts With Equal Sum Leetcode Solution
Difficulty: Easy
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])
.
Example 1: Input: arr = [0,2,1,-6,6,-7,9,1,2,0,1] Output: true Explanation: The array can be partitioned as [0, 2, 1], [-6, 6, -7, 9, 1], [2, 0, 1], where each group has a sum of 3.
Approach: To partition the array into, we need to first calculate the sum of all the elements in the array. If the sum is not divisible by 3, then we cannot partition the array into 3 non-empty parts with equal sums.
Otherwise, we need to find two positions in the array where we can partition it into 3 parts. To do that, we can use 2 pointers, left
and right
. We start from the first element of the array and the last element of the array respectively. We keep a track of the sum of elements from left to right and from right to left. If at any point the sum of the elements between left
and right
is equal to one-third of the total sum, we increment our counter of partitions. We keep doing this until the left
pointer is less than the right
pointer. If we are able to make 3 partitions, we return true
, else false
.
Algorithm:
- Initialize two pointers
left
andright
to the first and last index of the array respectively. - Calculate the sum of all the elements in the array and keep it in a variable called
totalSum
. - If
totalSum
is not divisible by 3, returnfalse
. - Initialize two variables
leftSum
andrightSum
to 0. These variables will keep track of the sum of elements from left to right and right to left. - Initialize a variable called
partitions
to 0. This variable will keep track of the number of partitions we have made so far. - While
left
is less thanright
: a. IfleftSum
is equal to one-third oftotalSum
, incrementpartitions
and resetleftSum
to 0. b. IfrightSum
is equal to one-third oftotalSum
, incrementpartitions
and resetrightSum
to 0. c. Addarr[left]
toleftSum
and move theleft
pointer to the right. d. Addarr[right]
torightSum
and move theright
pointer to the left. - If
partitions
is equal to 3, returntrue
, elsefalse
.
Code:
def canThreePartsEqualSum(arr: List[int]) -> bool: totalSum = sum(arr) if totalSum % 3 != 0: return False
left, right = 0, len(arr) - 1
leftSum, rightSum = 0, 0
partitions = 0
while left < right:
if leftSum == totalSum // 3:
partitions += 1
leftSum = 0
else:
leftSum += arr[left]
left += 1
if rightSum == totalSum // 3:
partitions += 1
rightSum = 0
else:
rightSum += arr[right]
right -= 1
if partitions == 3:
return True
return False
Time Complexity: The time complexity of this algorithm is O(n), where n is the length of the input array. We need to iterate over the entire array to calculate the sum and find the partition points.
Space Complexity: The space complexity of the algorithm is O(1). We are not using any extra space except for some variables to keep track of pointers, sum, and partitions.
Partition Array Into Three Parts With Equal Sum Solution Code
1