A logistics center processes packages on a conveyor belt represented by an integer array `weights`, where `weights[i]` is the weight of the $i$-th package. To optimize shipping, packages must be grouped into pairs. A pair of packages is considered a "target shipment" if their combined weight is exactly `target`. Both packages in a shipment must be selected from the same contiguous segment of the conveyor belt. Additionally, to ensure processing efficiency, we want to form at least `k` such pairwise disjoint target shipments (meaning no package can belong to more than one shipment). Find and return the minimum length of a contiguous conveyor belt segment (subarray) that contains at least `k` disjoint target shipments. If it is impossible to find such a segment, return `-1`.
Explanation: The contiguous subarray from index 2 to 6 is [4, 3, 5, 2, 3]. Within this segment, we can form 2 disjoint target shipments: the package at index 2 (weight 4) with the package at index 5 (weight 2), and the package at index 3 (weight 3) with the package at index 6 (weight 3). The length of this segment is 5. No shorter segment contains at least 2 disjoint shipments.
Explanation: To form 2 disjoint shipments of weight 4, we need four packages of weight 2. The shortest segment containing all of them is the entire array of length 4.
Explanation: There are no two packages on the conveyor belt that sum up to 10. Thus, we cannot form even 1 target shipment.
Master coding challenges related to Two Pointers and solve the Minimum Conveyor Section for K Target Shipments problem optimally.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak