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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an array of positive integers price where price[i] denotes the price of the ith candy and a positive integer k.
The store sells baskets of k distinct candies. The tastiness of a candy basket is the smallest absolute difference of the prices of any two candies in the basket.
Return the maximum tastiness of a candy basket.
Example 1:
Input: price = [13,5,1,8,21,2], k = 3 Output: 8 Explanation: Choose the candies with the prices [13,5,21]. The tastiness of the candy basket is: min(|13 - 5|, |13 - 21|, |5 - 21|) = min(8, 8, 16) = 8. It can be proven that 8 is the maximum tastiness that can be achieved.
Example 2:
Input: price = [1,3,1], k = 2 Output: 2 Explanation: Choose the candies with the prices [1,3]. The tastiness of the candy basket is: min(|1 - 3|) = min(2) = 2. It can be proven that 2 is the maximum tastiness that can be achieved.
Example 3:
Input: price = [7,7,7,7], k = 2 Output: 0 Explanation: Choosing any two distinct candies from the candies we have will result in a tastiness of 0.
Constraints:
2 <= k <= price.length <= 1051 <= price[i] <= 109Problem summary: You are given an array of positive integers price where price[i] denotes the price of the ith candy and a positive integer k. The store sells baskets of k distinct candies. The tastiness of a candy basket is the smallest absolute difference of the prices of any two candies in the basket. Return the maximum tastiness of a candy basket.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Greedy
[13,5,1,8,21,2] 3
[1,3,1] 2
[7,7,7,7] 2
container-with-most-water)sliding-window-maximum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2517: Maximum Tastiness of Candy Basket
class Solution {
public int maximumTastiness(int[] price, int k) {
Arrays.sort(price);
int l = 0, r = price[price.length - 1] - price[0];
while (l < r) {
int mid = (l + r + 1) >> 1;
if (check(price, k, mid)) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
private boolean check(int[] price, int k, int x) {
int cnt = 0, pre = -x;
for (int cur : price) {
if (cur - pre >= x) {
pre = cur;
++cnt;
}
}
return cnt >= k;
}
}
// Accepted solution for LeetCode #2517: Maximum Tastiness of Candy Basket
func maximumTastiness(price []int, k int) int {
sort.Ints(price)
return sort.Search(price[len(price)-1], func(x int) bool {
cnt, pre := 0, -x
for _, cur := range price {
if cur-pre >= x {
pre = cur
cnt++
}
}
return cnt < k
}) - 1
}
# Accepted solution for LeetCode #2517: Maximum Tastiness of Candy Basket
class Solution:
def maximumTastiness(self, price: List[int], k: int) -> int:
def check(x: int) -> bool:
cnt, pre = 0, -x
for cur in price:
if cur - pre >= x:
pre = cur
cnt += 1
return cnt >= k
price.sort()
l, r = 0, price[-1] - price[0]
while l < r:
mid = (l + r + 1) >> 1
if check(mid):
l = mid
else:
r = mid - 1
return l
// Accepted solution for LeetCode #2517: Maximum Tastiness of Candy Basket
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2517: Maximum Tastiness of Candy Basket
// class Solution {
// public int maximumTastiness(int[] price, int k) {
// Arrays.sort(price);
// int l = 0, r = price[price.length - 1] - price[0];
// while (l < r) {
// int mid = (l + r + 1) >> 1;
// if (check(price, k, mid)) {
// l = mid;
// } else {
// r = mid - 1;
// }
// }
// return l;
// }
//
// private boolean check(int[] price, int k, int x) {
// int cnt = 0, pre = -x;
// for (int cur : price) {
// if (cur - pre >= x) {
// pre = cur;
// ++cnt;
// }
// }
// return cnt >= k;
// }
// }
// Accepted solution for LeetCode #2517: Maximum Tastiness of Candy Basket
function maximumTastiness(price: number[], k: number): number {
price.sort((a, b) => a - b);
let l = 0;
let r = price[price.length - 1] - price[0];
const check = (x: number): boolean => {
let [cnt, pre] = [0, -x];
for (const cur of price) {
if (cur - pre >= x) {
pre = cur;
++cnt;
}
}
return cnt >= k;
};
while (l < r) {
const mid = (l + r + 1) >> 1;
if (check(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
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: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
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.