BackmediumArraysMicrosoft

Warehouse Order Optimization Solution

Problem Statement

Given a non‑decreasing integer array nums, find the maximum length of a contiguous subarray that satisfies two conditions: (1) its length is even, and (2) the sum of the first half equals the sum of the second half. Return that length; if no such subarray exists, return 0. An O(n log n) solution can be built using a divide‑and‑conquer approach analogous to merge sort or quick sort, where prefix sums are merged across recursive boundaries to test candidate even‑length windows efficiently.

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

Explanation: All even‑length windows are examined. The only windows where the two halves have equal sums are the length‑2 windows containing the pair [3,3]; each half sums to 3. No longer window meets the condition, so the answer is 2.

Example 2
Input
[5,5,5,5,5]
Output
4

Explanation: The array is sorted and all elements are identical. Any even‑length window has equal half‑sums. The longest possible even window is the first four elements, length 4, giving equal sums 5+5 = 10 on each side.

Example 3
Input
[-3,-3,-3,-3]
Output
4

Explanation: All elements are the same negative value. The whole array (length 4) forms a valid subarray because the first half sum = -3+(-3) = -6 equals the second half sum = -3+(-3) = -6. Hence the maximum length is 4.

Constraints

  • 1 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • nums is sorted in non‑decreasing order
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

Warehouse Order Optimization — Problem Statement & Solution Guide

ArraysMediumMerge Sort / Quick Sort
TimeO(n log n)
|
SpaceO(n)

Problem Description

Given a non‑decreasing integer array nums, find the maximum length of a contiguous subarray that satisfies two conditions: (1) its length is even, and (2) the sum of the first half equals the sum of the second half. Return that length; if no such subarray exists, return 0. An O(n log n) solution can be built using a divide‑and‑conquer approach analogous to merge sort or quick sort, where prefix sums are merged across recursive boundaries to test candidate even‑length windows efficiently.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Warehouse Order Optimization"

medium

WHY DOES IT MATTER?

Balancing two halves of a sequence appears in load‑balancing, memory partitioning, and financial reconciliation; mastering this pattern teaches you to turn a global equality constraint into local prefix‑sum differences that can be merged efficiently.

OPTIMIZATION CHALLENGE

The breakthrough is recognizing that equal‑half sums translate to a zero difference of prefix‑sum offsets, enabling a linear‑time merge that aggregates these differences instead of recomputing sums for every window.

REAL-WORLD CONNECTION

Think of a warehouse where incoming pallets must be split into two trucks with identical weight; the divide‑and‑conquer method mirrors how a logistics system recursively partitions shipments and then matches complementary weight differences at each merge point.

During an interview, compute the prefix‑sum array once, then focus on the recursive merge step – a simple hashmap or array indexed by difference values often eliminates the need for a full sort, keeping the implementation clean and fast.

COMPLEXITY AT A GLANCE

⏱ Time:O(n log n)
💾 Space:O(n)

Core Theory — Why This Approach?

The naive solution enumerates every possible even‑length window, computes the sum of its left half and right half, and checks equality – a double loop that costs O(n^2) time and quickly exceeds limits for n up to 10^5. The key observation for an optimal solution is that the condition "sum(first half)=sum(second half)" can be rewritten as "prefixSum[i‑1]‑prefixSum[l‑1] = prefixSum[r]‑prefixSum[i‑1]" where i is the midpoint of the window. By using a divide‑and‑conquer strategy similar to merge‑sort, we recursively solve the problem for left and right halves and then combine results by scanning possible midpoints while maintaining a map of prefix‑sum differences. This yields an O(n log n) algorithm because each level of recursion processes O(n) elements and the recursion depth is O(log n).

Interview Questions on This Problem

Q1How would you adapt the divide‑and‑conquer solution if the array were not sorted?

The algorithm does not rely on the non‑decreasing property; it only needs the prefix sums. Therefore the same O(n log n) approach works unchanged, but you must compute prefix sums first, which is O(n).

Q2Can you solve the problem in O(n) time using a different technique?

Yes. By scanning from the centre outward and storing the difference between left‑side and right‑side sums in a hash‑map for each possible centre, you can achieve O(n) average time, but the worst‑case still approaches O(n^2) without careful pruning, so the safe guaranteed bound remains O(n log n).

Q3Why does the even‑length requirement simplify the merging step in the divide‑and‑conquer approach?

Even length guarantees a unique centre index, so when merging the left and right solutions you only need to consider windows whose midpoint aligns with the border between the two halves, allowing a linear‑time two‑pointer sweep instead of handling odd‑length offsets.

Examples

Example 1

Input

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

Output

2

Explanation: All even‑length windows are examined. The only windows where the two halves have equal sums are the length‑2 windows containing the pair [3,3]; each half sums to 3. No longer window meets the condition, so the answer is 2.

Example 2

Input

[5,5,5,5,5]

Output

4

Explanation: The array is sorted and all elements are identical. Any even‑length window has equal half‑sums. The longest possible even window is the first four elements, length 4, giving equal sums 5+5 = 10 on each side.

Example 3

Input

[-3,-3,-3,-3]

Output

4

Explanation: All elements are the same negative value. The whole array (length 4) forms a valid subarray because the first half sum = -3+(-3) = -6 equals the second half sum = -3+(-3) = -6. Hence the maximum length is 4.

Constraints

  • 1 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • nums is sorted in non‑decreasing order

Optimal Approach & Strategy

Build a prefix‑sum array once, then apply a divide‑and‑conquer merge that records the difference between left and right half sums at each possible midpoint, using a hashmap to find matching differences in linear time per level, yielding O(n log n).

Brute Force Approach

Iterate over every possible even‑length subarray, compute the sum of its first half and second half, and keep the maximum length that satisfies equality – this costs O(n^2) time. The approach is simple but impractical for large inputs.

Verified Code Solutions

JavaScript Solution
Time: O(n log n)
const fs = require('fs');
const data = fs.readFileSync(0,'utf8').trim().split(/\s+/).map(Number);
let pos=0;
const n = data[pos++]||0;
const nums = data.slice(pos, pos+n);
function maxEvenLength(nums){
    const n = nums.length;
    const pref = new Array(n+1).fill(0);
    for(let i=0;i<n;++i) pref[i+1]=pref[i]+nums[i];
    const feasible = (L)=>{
        const half = L>>1;
        for(let i=0;i+L<=n;++i){
            const left = pref[i+half]-pref[i];
            const right= pref[i+L]-pref[i+half];
            if(left===right) return true;
        }
        return false;
    };
    let low=0, high=n - (n%2);
    while(low<high){
        let mid = Math.floor((low+high+2)/2);
        if(mid%2) ++mid;
        if(mid>high) mid=high;
        if(feasible(mid)) low=mid; else high=mid-2;
    }
    return low;
}
console.log(maxEvenLength(nums).toString());

Asked in Top Tech Interviews

Microsoft

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.