Partition Array Into Three Parts With Equal Sum
Easy
12
1
47.1% Acceptance
In this lab, you are tasked with implementing a function canThreePartsEqualSum
that takes an array of integers arr
and returns true
if the array can be partitioned into three non-empty parts with equal sums.
More specifically, we can partition the array if we can find indexes i + 1 < j
, such that:
(arr[0] + arr[1] + ... + arr[i] == arr[i + 1] + arr[i + 2] + ... + arr[j - 1] == arr[j] + arr[j + 1] + ... + arr[arr.length - 1])
Your implementation should use ESM import/export and pay attention to the evaluation script's requirements.
Example 1:
const arr = [0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1]; console.log(canThreePartsEqualSum(arr)); // Output: true // Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
Example 2:
const arr = [0, 2, 1, -6, 6, 7, 9, -1, 2, 0, 1]; console.log(canThreePartsEqualSum(arr)); // Output: false
Example 3:
const arr = [3, 3, 6, 5, -2, 2, 5, 1, -9, 4]; console.log(canThreePartsEqualSum(arr)); // Output: true // Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
Constraints:
3 <= arr.length <= 5 * 104
-104 <= arr[i] <= 104
Challenges
- Implement the
canThreePartsEqualSum
function and export it. Your function should take an arrayarr
of integers as input and return a boolean value as specified in the lab description. Don't forget to use ESM import/export. - Write tests for the
canThreePartsEqualSum
function in the evaluation script. Ensure that the tests cover various cases, including edge cases and possible errors.