BackmediumArraysPayPal

Balanced Subarray Length Solution

Problem Statement

Given a sorted array of integers weights, find the length of the longest subarray that can be divided into two halves with equal sums.

Example 1
Input
[1, 2, 3, 4, 5, 6, 7, 8]
Output
4

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5, 6, 7, 8], we first calculate the total sum of the array, which is 36. Then, we try to find the longest subarray that can be divided into two halves with equal sums. We start by finding the middle index of the array, which is 4. We then calculate the sum of the left half, which is 10 (1+2+3+4). We also calculate the sum of the right half, which is 26 (5+6+7+8). Since the sums are not equal, we move the middle index to the left by one position and repeat the process. We continue this process until we find a subarray that can be divided into two halves with equal sums, which is [1, 2, 3, 4] with a sum of 10 for both halves.

Example 2
Input
[1]
Output
1

Explanation: Step-by-step: Given the input [1], we first calculate the total sum of the array, which is 1. Then, we try to find the longest subarray that can be divided into two halves with equal sums. Since the array has only one element, it cannot be divided into two halves with equal sums, so the output is 1.

Constraints

  • Array length is between 1 and 1000
  • All elements in the array are integers between 1 and 1000
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 Subarray Length — Problem Statement & Solution Guide

ArraysMediumMerge Sort / Quick Sort
TimeO(n^2)
|
SpaceO(1)

Problem Description

Given a sorted array of integers weights, find the length of the longest subarray that can be divided into two halves with equal sums.

Examples

Example 1

Input

[1, 2, 3, 4, 5, 6, 7, 8]

Output

4

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5, 6, 7, 8], we first calculate the total sum of the array, which is 36. Then, we try to find the longest subarray that can be divided into two halves with equal sums. We start by finding the middle index of the array, which is 4. We then calculate the sum of the left half, which is 10 (1+2+3+4). We also calculate the sum of the right half, which is 26 (5+6+7+8). Since the sums are not equal, we move the middle index to the left by one position and repeat the process. We continue this process until we find a subarray that can be divided into two halves with equal sums, which is [1, 2, 3, 4] with a sum of 10 for both halves.

Example 2

Input

[1]

Output

1

Explanation: Step-by-step: Given the input [1], we first calculate the total sum of the array, which is 1. Then, we try to find the longest subarray that can be divided into two halves with equal sums. Since the array has only one element, it cannot be divided into two halves with equal sums, so the output is 1.

Constraints

  • Array length is between 1 and 1000
  • All elements in the array are integers between 1 and 1000

Optimal Approach & Strategy

The optimized approach uses a prefix sum array to efficiently calculate the sum of any subarray, reducing the time complexity to O(n log n) or O(n) using a sliding window technique or hash map. This approach involves iterating over the array and checking if any subarray can be divided into two equal halves.

Brute Force Approach

The brute-force approach involves checking all possible subarrays and dividing them into two halves to check if their sums are equal, resulting in a time complexity of O(n²). This approach is inefficient for large arrays. It can be implemented using nested loops to generate all possible subarrays.

Verified Code Solutions

JavaScript Solution
Time: O(n^2)
function balancedSubarrayLength(weights) {
  let maxLen = 0;
  for (let i = 0; i < weights.length; i++) {
    let leftSum = 0;
    for (let j = i; j < weights.length; j++) {
      leftSum += weights[j];
      let rightSum = 0;
      for (let k = j + 1; k < weights.length; k++) {
        rightSum += weights[k];
        if (leftSum === rightSum) {
          maxLen = Math.max(maxLen, j - i + 1);
        }
      }
    }
  }
  return maxLen;
}

Asked in Top Tech Interviews

PayPal

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.