<p>The <strong>distance of a pair</strong> of integers <code>a</code> and <code>b</code> is defined as the absolute difference between <code>a</code> and <code>b</code>.</p>

<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest <strong>distance among all the pairs</strong></em> <code>nums[i]</code> <em>and</em> <code>nums[j]</code> <em>where</em> <code>0 &lt;= i &lt; j &lt; nums.length</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,3,1], k = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> Here are all the pairs:
(1,3) -&gt; 2
(1,1) -&gt; 0
(3,1) -&gt; 2
Then the 1<sup>st</sup> smallest distance pair is (1,1), and its distance is 0.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,1], k = 2
<strong>Output:</strong> 0
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,6,1], k = 3
<strong>Output:</strong> 5
</pre>

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

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
	<li><code>1 &lt;= k &lt;= n * (n - 1) / 2</code></li>
</ul>

class Solution:
    def smallestDistancePair(self, nums: List[int], k: int) -> int:
        # After reading soultions
        min_num, max_num = min(nums), max(nums)
        counter = Counter(nums)
        num_items = sorted(list(counter.items()))

        left = 0
        right = max_num - min_num + 1
        while left < right:
            mid_distance = (left + right) // 2
            pair_num = self.compute_pair_below_distance(num_items, mid_distance)
            if k > pair_num:
                left = mid_distance + 1
            else:
                # The final result might be >= k
                right = mid_distance
        return left
    
    def compute_pair_below_distance(self, num_items, target_distance):
        total_pair = 0
        left, right = 0, 0
        curr_num = 0    # Current numbers waited for pairing
        to_pair_num = 0 # Number to be paired
        while right < len(num_items):
            left_num, left_counting = num_items[left]
            while right < len(num_items):
                right_num, right_counting = num_items[right]
                if right_num - left_num > target_distance:
                    break
                to_pair_num += right_counting
                right += 1 
            
            # Calculate the current pair num
            # curr_num + (curr_num + 1) + (curr_num + 2) ... + (curr_num + to_pair_num - 1)
            total_pair += (to_pair_num) * (curr_num * 2 + to_pair_num - 1) // 2
            curr_num = curr_num + to_pair_num # Add to pair set
            to_pair_num = 0
            # Remove number out of range
            while right_num - left_num > target_distance:
                curr_num -= left_counting
                left += 1
                left_num, left_counting = num_items[left]
        return total_pair
            

18055