BackmediumArrays

Balanced Boundary Partition Solution

Problem Statement

Given an integer array nums, you need to partition it into three non-empty contiguous subarrays: Left, Mid, and Right. The partition must satisfy the condition that the sum of the elements in the Left subarray is equal to the sum of the elements in the Right subarray. Your goal is to find the maximum possible sum of the Mid subarray among all valid partitions. If no such partition is possible, return -10^9.

Example 1
Input
[1, 2, 3, 2, 1]
Output
3

Explanation: Step-by-step: with input [1, 2, 3, 2, 1], we can partition it into [1, 2], [3], and [2, 1]. The sum of the elements in the Left subarray [1, 2] is 3, which is equal to the sum of the elements in the Right subarray [2, 1]. The sum of the elements in the Mid subarray [3] is 3, which is the maximum possible sum among all valid partitions.

Example 2
Input
[1, 1, 1, 1, 1]
Output
1

Explanation: Step-by-step: with input [1, 1, 1, 1, 1], we can partition it into [1, 1], [1], and [1, 1]. The sum of the elements in the Left subarray [1, 1] is 2, which is equal to the sum of the elements in the Right subarray [1, 1]. The sum of the elements in the Mid subarray [1] is 1, which is the maximum possible sum among all valid partitions.

Constraints

  • 3 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • The cumulative sums and the output fit within a standard 32-bit signed integer.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Balanced Boundary Partition — Problem Statement & Solution Guide

ArraysMediumcumulative-array-sum
TimeO(n^2)
|
SpaceO(n)

Problem Description

Given an integer array nums, you need to partition it into three non-empty contiguous subarrays: Left, Mid, and Right. The partition must satisfy the condition that the sum of the elements in the Left subarray is equal to the sum of the elements in the Right subarray. Your goal is to find the maximum possible sum of the Mid subarray among all valid partitions. If no such partition is possible, return -10^9.

Examples

Example 1

Input

[1, 2, 3, 2, 1]

Output

3

Explanation: Step-by-step: with input [1, 2, 3, 2, 1], we can partition it into [1, 2], [3], and [2, 1]. The sum of the elements in the Left subarray [1, 2] is 3, which is equal to the sum of the elements in the Right subarray [2, 1]. The sum of the elements in the Mid subarray [3] is 3, which is the maximum possible sum among all valid partitions.

Example 2

Input

[1, 1, 1, 1, 1]

Output

1

Explanation: Step-by-step: with input [1, 1, 1, 1, 1], we can partition it into [1, 1], [1], and [1, 1]. The sum of the elements in the Left subarray [1, 1] is 2, which is equal to the sum of the elements in the Right subarray [1, 1]. The sum of the elements in the Mid subarray [1] is 1, which is the maximum possible sum among all valid partitions.

Constraints

  • 3 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • The cumulative sums and the output fit within a standard 32-bit signed integer.

Optimal Approach & Strategy

Precalculate prefix sums and store the first and last occurrence indices of each sum value in a hash map. For every possible partition, verify if the prefix sum at the start of the right segment matches a prefix sum captured earlier, allowing for O(n) traversal using the map.

Brute Force Approach

Iterate through all possible cut points i and j where 0 < i < j < n-1, calculate the sum of Left (0 to i-1), Mid (i to j-1), and Right (j to n-1). Check if sum(Left) == sum(Right) and update the maximum Mid sum.

Verified Code Solutions

JavaScript Solution
Time: O(n^2)
function solution(nums) { let maxSum = -Infinity; for (let i = 1; i < nums.length - 1; i++) { for (let j = i + 1; j < nums.length; j++) { let leftSum = nums.slice(0, i).reduce((a, b) => a + b, 0); let midSum = nums.slice(i, j).reduce((a, b) => a + b, 0); let rightSum = nums.slice(j).reduce((a, b) => a + b, 0); if (leftSum === rightSum) { maxSum = Math.max(maxSum, midSum); } } } return maxSum; }

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.