Balanced Subarray Length ā Problem Statement & Solution Guide
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
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.
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
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;
}class Solution {
public int balancedSubarrayLength(int[] weights) {
if (weights.length <= 1) {
return weights.length;
}
int totalSum = 0;
for (int weight : weights) {
totalSum += weight;
}
if (totalSum % 2 != 0) {
return 0;
}
int targetSum = totalSum / 2;
int leftSum = 0;
int maxLength = 0;
for (int i = 0; i < weights.length; i++) {
leftSum += weights[i];
for (int j = i + 1; j < weights.length; j++) {
int rightSum = totalSum - leftSum;
if (leftSum == rightSum) {
maxLength = Math.max(maxLength, j - i + 1);
}
}
}
return maxLength;
}
}def balanced_subarray_length(weights):
if len(weights) <= 1:
return len(weights)
total_sum = sum(weights)
if total_sum % 2 != 0:
return 0
target_sum = total_sum // 2
left_sum = 0
max_length = 0
for i in range(len(weights)):
left_sum += weights[i]
for j in range(i + 1, len(weights)):
right_sum = total_sum - left_sum
if left_sum == right_sum:
max_length = max(max_length, j - i + 1)
return max_lengthfunction 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
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.