Maximum Tastiness of Candy Basket

You are given an array of positive integers price where price[i] denotes the price of the i^th 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
Inputprice = [13,5,1,8,21,2], k = 3
Output8
Choose candies with prices [13, 5, 21], whose pairwise absolute differences have minimum 8, and it can be proven that 8 is the maximum tastiness achievable.
Example 2
Inputprice = [1,3,1], k = 2
Output2
Choose candies with prices [1, 3], whose only absolute price difference is 2, and it can be proven that 2 is the maximum tastiness achievable.

Constraints

  • 2 <= k <= price.length <= 10^5
  • 1 <= price[i] <= 10^9

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate