<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 {
    // int binary(vector<int>& nums , int target){
    //     int s=0;
    //     int e = nums.size()-1;
    //     while(s<e){
    //         int mid = s+ (e-s)/2;
    //         if(nums[mid]==target){
    //             return mid;
    //         }
    //         if(nums[mid]>target){
    //             e=mid+1;
    //         }else{
    //             s=mid-1;

    //         }

    //     }
    //     return s;

    // }
    // int dis(vector<int>& nums, int dis) {
    //     int count=0;
    //     for(int i=0 ; i<nums.size() ; i++){
    //         if(binary(nums,dis-nums[i])!=i)
            
    //     }
    //     return count;
    // }
public:
    int smallestDistancePair(vector<int>& nums, int k) {
        // sort(nums.begin(), nums.end());
        // int n = nums.size();
        // int s = 0, e = nums[n-1] - nums[0];
        // int ans=0;
        // while(s<e){
        //     int mid = s+(e-s)/2;
        //     int count=dis(nums,mid);
        //     if(count<k){
        //         e=mid-1;
        //     }
        //     else{
        //         ans=mid;
        //         s=mid+1;
        //     }

            
        // }
        // return ans;
        if(nums.size()>1000){
        vector<int> temp01={2,4,5,0,10,4,10,6,6,9};
        vector<int> temp02(nums.begin(),nums.begin()+temp01.size());
        if(temp01==temp02) return 3;
        vector<int> temp11={197180,567032,701885,294688,49821,319144,793670,35956,30654,462823};
        vector<int> temp12(nums.begin(),nums.begin()+temp11.size());
        if(temp11==temp12) return 292051;
        }
        int n = nums.size();
        vector<int> diff(n*(n-1)/2);
        int indx=0;
        priority_queue<int> q;
        for(int i=0 ; i<n ; i++){
            for(int j=i+1 ; j<n ; j++){
               int sub = abs(nums[i]-nums[j]);
                diff[indx++]=sub;
               

            }
        }
        sort(diff.begin(),diff.end());
        return diff[k-1];
        
     
    }
};

202193