Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an integer array prices representing the prices of various chocolates in a store. You are also given a single integer money, which represents your initial amount of money.
You must buy exactly two chocolates in such a way that you still have some non-negative leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.
Return the amount of money you will have leftover after buying the two chocolates. If there is no way for you to buy two chocolates without ending up in debt, return money. Note that the leftover must be non-negative.
Example 1:
Input: prices = [1,2,2], money = 3 Output: 0 Explanation: Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0.
Example 2:
Input: prices = [3,2,3], money = 3 Output: 3 Explanation: You cannot buy 2 chocolates without going in debt, so we return 3.
Constraints:
2 <= prices.length <= 501 <= prices[i] <= 1001 <= money <= 100Problem summary: You are given an integer array prices representing the prices of various chocolates in a store. You are also given a single integer money, which represents your initial amount of money. You must buy exactly two chocolates in such a way that you still have some non-negative leftover money. You would like to minimize the sum of the prices of the two chocolates you buy. Return the amount of money you will have leftover after buying the two chocolates. If there is no way for you to buy two chocolates without ending up in debt, return money. Note that the leftover must be non-negative.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,2,2] 3
[3,2,3] 3
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2706: Buy Two Chocolates
class Solution {
public int buyChoco(int[] prices, int money) {
Arrays.sort(prices);
int cost = prices[0] + prices[1];
return money < cost ? money : money - cost;
}
}
// Accepted solution for LeetCode #2706: Buy Two Chocolates
func buyChoco(prices []int, money int) int {
sort.Ints(prices)
cost := prices[0] + prices[1]
if money < cost {
return money
}
return money - cost
}
# Accepted solution for LeetCode #2706: Buy Two Chocolates
class Solution:
def buyChoco(self, prices: List[int], money: int) -> int:
prices.sort()
cost = prices[0] + prices[1]
return money if money < cost else money - cost
// Accepted solution for LeetCode #2706: Buy Two Chocolates
impl Solution {
pub fn buy_choco(mut prices: Vec<i32>, money: i32) -> i32 {
prices.sort();
let cost = prices[0] + prices[1];
if cost > money {
return money;
}
money - cost
}
}
// Accepted solution for LeetCode #2706: Buy Two Chocolates
function buyChoco(prices: number[], money: number): number {
prices.sort((a, b) => a - b);
const cost = prices[0] + prices[1];
return money < cost ? money : money - cost;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.