AMAZON Coding Question β Solved
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.