Minimum Relative Loss After Buying Chocolates - Problem

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

Alice and Bob are at a chocolate shop and have agreed on a payment strategy:

  • If a chocolate costs โ‰ค ki, Bob pays the full price
  • If a chocolate costs > ki, Bob pays ki and Alice pays the remainder

For each query, Bob wants to select exactly mi chocolates to minimize his relative loss, defined as bi - ai where bi is Bob's total payment and ai is Alice's total payment.

Return an array where the i-th element is Bob's minimum possible relative loss for query i.

Input & Output

example_1.py โ€” Basic Case
$ Input: prices = [8, 15, 12, 6], queries = [[10, 2]]
โ€บ Output: [5]
๐Ÿ’ก Note: For k=10, m=2: Bob wants to select 2 chocolates. For chocolate costing 8โ‰ค10, Bob pays 8, Alice pays 0. For chocolate costing 15>10, Bob pays 10, Alice pays 5. Best selection is chocolates costing 15 and 6: Bob pays 10+6=16, Alice pays 5+0=5, relative loss = 16-5 = 11. Wait, let me recalculate... Actually the optimal is chocolates 15 and 6 giving loss = (10+6) - (5+0) = 11. But we need to check all combinations systematically.
example_2.py โ€” Multiple Queries
$ Input: prices = [5, 10, 15, 20], queries = [[12, 2], [8, 3]]
โ€บ Output: [2, 18]
๐Ÿ’ก Note: Query 1: k=12, m=2. Best selection minimizes Bob's relative loss. Query 2: k=8, m=3. Bob has a lower limit, so more chocolates will require Alice's contribution.
example_3.py โ€” Edge Case
$ Input: prices = [1, 2, 3], queries = [[5, 3]]
โ€บ Output: [6]
๐Ÿ’ก Note: All chocolates cost โ‰ค k=5, so Bob pays full price for all: 1+2+3=6, Alice pays 0, relative loss = 6-0 = 6.

Constraints

  • 1 โ‰ค prices.length โ‰ค 105
  • 1 โ‰ค prices[i] โ‰ค 109
  • 1 โ‰ค queries.length โ‰ค 105
  • 1 โ‰ค ki โ‰ค 109
  • 1 โ‰ค mi โ‰ค prices.length

Visualization

Tap to expand
Optimal Chocolate Selection StrategyBeneficial (Alice pays more)Neutral (Split payment)Costly (Bob pays all)Chocolate Analysis (k = $10):$15Bob: $10Alice: $5Net: +5$12Bob: $10Alice: $2Net: +8$10Bob: $10Alice: $0Net: 0$8Bob: $8Alice: $0Net: -8$6Bob: $6Alice: $0Net: -6Optimal Selection (m = 2)$15SELECTED$12SELECTEDTotal Relative Loss: -3(Bob gains $3 compared to Alice)
Understanding the Visualization
1
Calculate Impact
For each chocolate, determine its contribution to Bob's relative loss
2
Rank Choices
Sort chocolates by their impact - prefer items where Alice pays more
3
Select Optimally
Choose the m chocolates with the best impact on relative loss
Key Takeaway
๐ŸŽฏ Key Insight: Transform the problem from finding combinations to ranking items by their contribution to relative loss. Expensive items (> k) actually help Bob because Alice pays more than he does!
Asked in
Google 23 Amazon 18 Microsoft 15 Meta 12
26.2K Views
Medium Frequency
~35 min Avg. Time
847 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