BackmediumArrays Microsoft

Warehouse Order Optimization Solution

Problem Statement

At a warehouse, crates are sorted by weight before shipping. Given a sorted array of crate weights, extract the longest subarray that has the same total weight in both halves using the merge sort or quick sort algorithm, and return the length of the extracted subarray.

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

Explanation: Step-by-step: Given the input array [1, 2, 3, 4, 5], we first find the middle index. Since the array has an odd length, we include the middle element in the left half. We then calculate the sum of the left half and the right half. If the sums are equal, we return the length of the subarray. In this case, the subarray [1, 2, 3] has equal halves and it's the longest one, so the output is 3.

Example 2
Input
[5, 5]
Output
2

Explanation: Step-by-step: Given the input array [5, 5], we first find the middle index. Since the array has an even length, we split it into two halves. We then calculate the sum of the left half and the right half. If the sums are equal, we return the length of the subarray. In this case, the subarray [5, 5] has equal halves and it's the longest one, so the output is 2.

Constraints

  • Array length is between 1 and 1000
  • All elements in the array are integers between 1 and 1000
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free