Problem · Array
Reduce Gifts
Learn this problemProblem statement
New Year's Day is around the corner and Amazon is having a sale. They have a list of items they are considering but they may need to remove some of them. Determine the minimum number of items to remove from an array of prices so that the sum of prices of any k items does not exceed a threshold.
Note: If the number of items in the list is less than k, then there is no need to remove any more items.
Function
reduceGifts(prices: int[], k: int, threshold: int) → intComplete the function reduceGifts in the editor.
reduceGifts has the following parameters:
int prices[n]: the prices of each itemint k: the number of items to sumint threshold: the maximum allowed price sum ofkitems
Returns
int: the minimum number of items to remove
Examples
Example 1
prices = [3, 2, 1, 4, 6, 5]k = 3threshold = 14return = 1
The sum of prices for every
k = 3 items must not be more than threshold = 14. The sum of the prices of the last three items is 6 + 5 + 4 = 15. The item priced $6 can be removed leaving:
No 3 items' prices sum to greater than 14. Only 1 item needs to be removed.Example 2
prices = [9, 6, 7, 2, 7, 2]k = 2threshold = 13return = 2Items with prices 9 and 7 have a sum larger than 13. After removing these two items, prices' = [6, 2, 7, 2].
No k items have a sum of prices greater than threshold.
Example 3
prices = [9, 6, 3, 2, 9, 10, 10, 11]k = 4threshold = 1return = 5Since no price is less than or equal to threshold, the sum of any k elements will always be greater than threshold.
The threshold only applies to goups of k items. Now the goal is to reduce the arr length to k - 1 which requires the removal of 5 elements.
Constraints
1 <= k <= n <= 1051 <= threshold <= 1091 <= prices[i] <= 109
More Amazon problems
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026