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.
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.
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.
Master coding challenges related to Arrays and solve the [Backup] Warehouse Order Optimization problem optimally.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak