Mid/SeniorArrayPrefix Sum

Split Array With Minimum Difference

You are given an integer array nums.

Split the array into exactly two subarrays, left and right, such that:

  • left is strictly increasing.
  • right is strictly decreasing.

Return the minimum possible absolute difference between the sums of left and right. If no valid split exists, return -1.

Example 1
Inputnums = [1,3,2]
Output2
The valid splits have absolute differences 4 and 2, so the minimum absolute difference is 2.
Example 2
Inputnums = [1,2,4,3]
Output4
The valid splits have absolute differences 4 and 4, so the minimum absolute difference is 4.

Constraints

  • 2 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5

Asked at 1 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