Partition Array Into Three Parts With Equal Sum

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 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]

are all equal.

Example 1
Inputarr = [0,2,1,-6,6,-7,9,1,2,0,1]
Outputtrue
0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1.
Example 2
Inputarr = [0,2,1,-6,6,7,9,-1,2,0,1]
Outputfalse
The array cannot be partitioned into three non-empty parts with equal sums.

Constraints

  • 3 <= arr.length <= 5 * 10^4
  • -10^4 <= arr[i] <= 10^4

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate