##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2517.html

# 2517. Maximum Tastiness of Candy Basket

## Description

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 <= 105
• 1 <= price[i] <= 109

## Solutions

• class Solution {
private int[] price;
private int k;

public int maximumTastiness(int[] price, int k) {
Arrays.sort(price);
this.price = price;
this.k = k;
int left = 0, right = 1000000000;
while (left < right) {
int mid = (left + right + 1) >> 1;
if (check(mid)) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}

private boolean check(int x) {
int s = price;
int cnt = 1;
for (int i = 1; i < price.length; ++i) {
if (price[i] - s >= x) {
s = price[i];
++cnt;
}
}
return cnt >= k;
}
}

• class Solution {
public:
int maximumTastiness(vector<int>& price, int k) {
sort(price.begin(), price.end());
int left = 0, right = 1e9;
auto check = [&](int x) {
int s = price;
int cnt = 1;
for (int i = 1; i < price.size(); ++i) {
if (price[i] - s >= x) {
s = price[i];
++cnt;
}
}
return cnt >= k;
};
while (left < right) {
int mid = (left + right + 1) >> 1;
if (check(mid)) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
};

• class Solution:
def maximumTastiness(self, price: List[int], k: int) -> int:
def check(x):
cnt = 1
s = price
for p in price[1:]:
if p - s >= x:
s = p
cnt += 1
return cnt >= k

price.sort()
left, right = 0, 10**9
while left < right:
mid = (left + right + 1) >> 1
if check(mid):
left = mid
else:
right = mid - 1
return left


• func maximumTastiness(price []int, k int) int {
sort.Ints(price)
check := func(x int) bool {
s := price
cnt := 1
for _, p := range price[1:] {
if p-s >= x {
s = p
cnt++
}
}
return cnt >= k
}
left, right := 0, 1000000000
for left < right {
mid := (left + right + 1) >> 1
if check(mid) {
left = mid
} else {
right = mid - 1
}
}
return left
}

• function maximumTastiness(price: number[], k: number): number {
price.sort((a, b) => a - b);
let l = 0;
let r = price[price.length - 1] - price;
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;
}


• public class Solution {
public int MaximumTastiness(int[] price, int k) {
Array.Sort(price);
int l = 0, r = price[price.Length - 1] - price;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (check(price, mid, k)) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}

private bool check(int[] price, int x, int k) {
int cnt = 0, pre = -x;
foreach (int cur in price) {
if (cur - pre >= x) {
++cnt;
pre = cur;
}
}
return cnt >= k;
}
}