Minimum Relative Loss After Buying Chocolates - Problem

You are given an integer array prices which shows the chocolate prices, and a 2D integer array queries, where queries[i] = [ki, mi].

Alice and Bob went to buy some chocolates. For each query, they follow these payment terms:

  • If the price of a chocolate is less than or equal to ki, Bob pays for it entirely
  • Otherwise, Bob pays ki of it, and Alice pays the rest

Bob wants to select exactly mi chocolates such that his relative loss is minimized. The relative loss is defined as bi - ai, where ai is the total amount Alice paid and bi is the total amount Bob paid.

Return an integer array ans where ans[i] is Bob's minimum relative loss possible for queries[i].

Input & Output

Example 1 — Basic Case
$ Input: prices = [3,7,2,9], queries = [[5,2]]
Output: [3]
💡 Note: For k=5, m=2: Bob pays min(price,5) for each chocolate. Selecting chocolates with prices 2 and 9: Bob pays 2+5=7, Alice pays 0+4=4, relative loss = 7-4 = 3.
Example 2 — Multiple Queries
$ Input: prices = [1,4,8,2], queries = [[3,1],[6,2]]
Output: [1,0]
💡 Note: Query 1 (k=3,m=1): Contributions are [1,4,2*3-8=-2,2]=[1,4,-2,2]. Best choice is price=8 with contribution=-2, so relative loss=1. Query 2 (k=6,m=2): Contributions are [1,4,4,2]. Best choices give contributions 1+2=3, so relative loss=0.
Example 3 — Large k Value
$ Input: prices = [2,8,1,5], queries = [[10,2]]
Output: [3]
💡 Note: With k=10 (larger than all prices), Bob always pays full price. Best selection: prices 1,2. Bob pays 1+2=3, Alice pays 0, relative loss = 3-0 = 3.

Constraints

  • 1 ≤ prices.length ≤ 105
  • 1 ≤ prices[i] ≤ 109
  • 1 ≤ queries.length ≤ 105
  • 1 ≤ ki, mi ≤ 109
  • mi ≤ prices.length

Visualization

Tap to expand
Minimum Relative Loss After Buying Chocolates INPUT prices array: 3 7 2 9 [0] [1] [2] [3] queries: [[5, 2]] k = 5 (Bob's limit) m = 2 (chocolates to buy) Bob Pays first Alice Pays rest Loss = Bob - Alice ALGORITHM STEPS 1 Sort Prices sorted: [2, 3, 7, 9] 2 Build Prefix Sums prefix: [0,2,5,12,21] 3 Binary Search Find optimal split for k=5: idx=2 4 Calculate Loss Select m=2 chocolates Choose: prices 2 and 3 Both <= k=5 Bob pays: 2 + 3 = 5 Alice pays: 0 Loss: 5 - 0 - 2 = 3 FINAL RESULT Selected Chocolates: 2 3 Both paid by Bob (price <= 5) Payment Summary Bob's total (b): 5 Alice's total (a): 0 Relative Loss = b - a = 5 - 0 - 2 = 3 Output: [3] OK - Minimum Loss! Key Insight: Use prefix sums on sorted prices for O(1) range sum queries. Binary search finds the optimal split: take cheap chocolates (Bob pays full) from left, expensive ones (Alice helps) from right. For price <= k: loss = price. For price > k: loss = k - (price - k) = 2k - price. TutorialsPoint - Minimum Relative Loss After Buying Chocolates | Optimized with Prefix Sums
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen