AMAZON Coding Question – Solved

5 Live
The supply chain manager at one of Amazon's warehouses is shipping the last container of the day. All n boxes have been loaded into the truck with their sizes represented in the array `boxes`. The truck may not have enough space to store all the boxes, so some boxes may need to be unloaded. The remaining boxes must satisfy the condition: `max(boxes) ≤ space * min(boxes)`. Given the array `boxes` and an integer `space`, find the minimum number of boxes that need to be unloaded. Example: Given n = 4, boxes = [1, 4, 3, 2], and space = 2. This set already satisfies the condition, so the answer is 1 (minimum boxes to remove is 1). Function Description: Complete the function `findMaxUnloaded` in the editor below. `findMaxUnloaded` has the following parameters: - int boxes[n]: array representing the sizes of each box - int space: the multiplier Returns: - int: the minimum number of boxes to remove from the truck Constraints: - 1 ≤ n ≤ 10^5 - 1 ≤ boxes[i] ≤ 5 × 10^5 - 1 ≤ space ≤ 1000 Sample Input: n = 6 boxes = [4, 5, 3, 8, 3, 7] space = 2 Sample Output: 2 Explanation: Remove boxes 4 and 6 (sizes 8 and 7). Then the maximum size of the remaining boxes is 5, the minimum size is 3, and 5 ≤ 3 * 2. Alternatively, removing boxes 3 and 5 (both size 3) leaves max = 8 and min = 4, satisfying 8 ≤ 4 * 2. So, the minimum boxes to remove is 2.

Asked in: AMAZON

Image of the Question

Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


import bisect
def findMaxUnloaded(boxes, space):
    boxes.sort()    
    n = len(boxes)
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem, the core challenge is to identify a subset of boxes whose maximum size is at most `space` times the minimum size, and to minimize how many boxes we remove to achieve that condition.

Let's break down the problem and think about the key insights and strategies.

1. **Understanding the condition:**
The condition `max(boxes) ≤ space * min(boxes)` means that the ratio between the largest and smallest box sizes in the remaining set should be at most `space`. If the boxes in the subset are tightly clustered in size (within a multiplicative factor of `space`), the condition is met.

2. **Goal interpretation:**
We want to find the *largest subset* of boxes that satisfy this ratio condition, because minimizing removals is equivalent to maximizing the size of the valid subset.

3. **Sort the boxes:**
Since the condition involves max and min, sorting the array of box sizes is a natural first step. After sorting, the minimum box in any contiguous subset is the first element of that subset, and the maximum is the last element.

This reduces the problem to finding a contiguous subsequence (or subarray) in the sorted array where `max ≤ space * min`. Since the array is sorted, the minimum is the left endpoint, and the maximum is the right endpoint.

4. **Using two pointers or sliding window:**
Because we want to find the longest contiguous subsequence satisfying the condition, a sliding window approach is appropriate:

- Maintain two pointers, `left` and `right`, initially both at the start of the sorted array.
- Expand `right` pointer step-by-step to include boxes.
- For each new position of `right`, check if the current window `[left, right]` satisfies the condition:
- Check if `boxes[right] ≤ space * boxes[left]`.
- If it does, update the maximum window size found.
- If it does not, move `left` forward to reduce the window size until the condition holds again.

5. **Why contiguous windows after sorting?**
Sorting arranges boxes by size, so the minimum of any subset is at the left end, and the maximum at the right. Because the condition is about the ratio of max to min, checking contiguous subarrays in sorted order covers all possibilities without missing any subsets.

6. **Compute result:**
After processing the entire array, the maximum size of the valid window is known. The minimum boxes to unload is simply `n - max_window_size`, since the rest must be removed.

7. **Edge cases:**
- If `space` is very large, possibly the entire array satisfies the condition, so zero removals.
- If all boxes are the same size, condition trivially holds.
- If the array is very diverse, some boxes must be removed, but the sliding window ensures the largest possible valid subset.

8. **Time complexity:**
Sorting takes O(n log n). The sliding window is O(n) since each pointer moves at most n times. So overall O(n log n) time.

9. **Intuition summary:**
By sorting, you create a natural order to check for contiguous valid ranges. The two-pointer window technique efficiently finds the longest window where the largest box is at most `space` times the smallest. Then the difference between total boxes and this window size is the minimal removal count.

In summary, think about transforming the problem into finding the longest subarray in sorted order that satisfies a multiplicative ratio condition on the first and last elements, then use two pointers to identify this maximal subarray efficiently.
```


Related Questions