<p>You are given an array of positive integers <code>price</code> where <code>price[i]</code> denotes the price of the <code>i<sup>th</sup></code> candy and a positive integer <code>k</code>.</p>

<p>The store sells baskets of <code>k</code> <strong>distinct</strong> candies. The <strong>tastiness</strong> of a candy basket is the smallest absolute difference of the <strong>prices</strong> of any two candies in the basket.</p>

<p>Return <em>the <strong>maximum</strong> tastiness of a candy basket.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> price = [13,5,1,8,21,2], k = 3
<strong>Output:</strong> 8
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> price = [1,3,1], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> price = [7,7,7,7], k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> Choosing any two distinct candies from the candies we have will result in a tastiness of 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= k &lt;= price.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= price[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def maximumTastiness(self, price: List[int], k: int) -> int:  
        def check_possibility(tastiness):
            curr, j = min_price, 0

            count = 1
            while count < k:
                j += 1
                if j > len(price) - 1:
                    return False

                if price[j] - curr >= tastiness:
                    curr = price[j]
                    count += 1

            return True

        price = sorted(price)
        min_price = price[0]
        max_price = price[-1]
        
        right = (max_price - min_price) // (k - 1)
        left = 0

        while left <= right:
            mid = (left + right) // 2
            # print(left, right, mid)
            if check_possibility(mid):
                left = mid + 1
            else:
                right = mid - 1
        
        return right

        # def find_next_index(value, left):
        #     right = len(price) - 1
        #     while left <= right:
        #         mid = (left + right) // 2
        #         if price[mid] == value:
        #             return mid
        #         if price[mid] > value:
        #             right = mid - 1
        #         else:
        #             left = mid + 1

        #     return left       

        # def check_possibility(tastiness):
        #     curr, j = min_price, 0

        #     count = 1
        #     while count < k:
        #         if curr + tastiness > max_price:
        #             return False
                
        #         j = find_next_index(curr + tastiness, j)
        #         curr = price[j]
        #         count += 1

        #     return True

        # price = sorted(price)
        # min_price = price[0]
        # max_price = price[-1]
        
        # right = (max_price - min_price) // (k - 1)
        # left = 0

        # while left <= right:
        #     mid = (left + right) // 2
        #     # print(left, right, mid)
        #     if check_possibility(mid):
        #         left = mid + 1
        #     else:
        #         right = mid - 1
        
        # return right

        # def check_possibility(tastiness):
        #     curr = min_price

        #     count = 1
        #     while count < k:
        #         curr += tastiness
        #         while curr not in price_set:
        #             curr += 1
        #             if curr > max_price:
        #                 return False
        #         count += 1

        #     return True

        # min_price = min(price)
        # max_price = max(price)
        # price_set = set(price)
        
        # right = (max_price - min_price) // (k - 1)
        # left = 0

        # while left <= right:
        #     mid = (left + right) // 2
        #     # print(left, right, mid)
        #     if check_possibility(mid):
        #         left = mid + 1
        #     else:
        #         right = mid - 1
        
        # return right
923