<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 {
public:
    int countPairs(vector<int> &nums, int targetDistance){
        int count = 0, left = 0;
        for(int right = 1;right<nums.size();right++){
            while(nums[right] - nums[left]>targetDistance) left++;
            count += right - left;
        }
        return count;
    }
    // Count number of pairs with distance <= m
    // int countPairs(int maxDistance, vector<int>& nums, vector<int>& prefixCount,
    //                unordered_map<int, int>& valueCount) {
    //     int count = 0;
    //     int arraySize = nums.size();
    //     int index = 0;

    //     while (index < arraySize) {
    //         int currentValue = nums[index];
    //         int valueCountForCurrent = valueCount.at(currentValue);

    //         // Calculate pairs involving current value with distance <=
    //         // maxDistance
    //         int pairsWithLargerValues =
    //             prefixCount[currentValue + maxDistance] -
    //             prefixCount[currentValue];
    //         int pairsWithSameValues =
    //             valueCountForCurrent * (valueCountForCurrent - 1) / 2;

    //         count += pairsWithLargerValues * valueCountForCurrent +
    //                  pairsWithSameValues;

    //         // Skip duplicate values
    //         while (index + 1 < arraySize && nums[index] == nums[index + 1]) {
    //             ++index;
    //         }
    //         ++index;
    //     }
    //     return count;
    // }
    // int countPairs(int distance, vector<int> &nums, vector<int> &prefixCount, unordered_map<int,int> &valueCount){
    //     // int allowedNumbers = 0;
    //     // int totalNumber = (prefixCount[distance] * (prefixCount[distance] - 1)) / 2;
    //     // for(auto &i:nums){
    //     //     int ts = (valueCount[i]-1);
    //     //     allowedNumbers -= (ts * (totalNumber - ts));
    //     // }
    //     // allowedNumbers+=totalNumber;
    //     // cout<<distance<<" "<<totalNumber<<" "<<allowedNumbers<<endl;
    //     // return allowedNumbers;

    //     int count = 0;
    //     int arraySize = nums.size();
    //     int index = 0;
    //     while(index < arraySize){
    //         int currentValue = nums[index];
    //         int valueCountForCurrent = valueCount[currentValue];

    //         int pairsWithLargerValues = prefixCount[currentValue+maxDistance] - prefixCount[currentValue];

    //     }
    // }
    int smallestDistancePair(vector<int>& nums, int k) {
        sort(nums.begin(),nums.end());
        int arraySize = nums.size(), maxElement = nums[arraySize - 1],  maxPossibleDistance = maxElement * 2;
        vector<int> prefixCount(maxPossibleDistance,0);
        unordered_map<int,int> valueCount;
        for(auto &i:nums) valueCount[i]++;
        for(int i=1;i<maxPossibleDistance;i++){
            // cout<<i<<" "<<prefixCount[i]<<" "<<prefixCount[i-1]<<" "<<valueCount[i]<<endl;
            prefixCount[i] = prefixCount[i-1]+valueCount[i];
            // prefixCount 3 = prefixCount 2 + valueCount 3 3
            // prefixCount 2 = prefixCount 1 + valueCount 2 2
            // prefixCount 1 = prefixCount 0 + valueCount 1 2
        }
        // for(auto &i: prefixCount) cout<<i<<" ";
        // cout<<endl;
        // for(auto &i: valueCount) cout<<i.first<<" "<<i.second<<endl;
        int low = 0, high = maxElement;
        while(low<=high){
            int mid = low + (high - low) / 2;
            // if(countPairs(mid, nums, prefixCount, valueCount) < k){
            if(countPairs(nums,mid) < k){
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return low;
    }
};
411240