<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:

    /// distinct sequence
    int b[3000300];
    /// count number of each value in sequence 
    int tl[3000300];
    ///d[i]: count number of value that greater or equal to i 
    int d[3000300];
    int n;

    int smallestDistancePair(vector<int>& a, int k) {
        n = a.size();
        for (int i = 0; i < n; ++ i)
            a[i] = a[i] + 1;
        /// count 
        for (int i = 0; i < n; ++ i){
            d[a[i]]++;
            tl[a[i]]++;
            ///kt[a[i]] = 1;
        }
        sort(a.begin(), a.end());
        int maxx = a[n - 1];
        for (int i = 1; i <= maxx * 2; ++ i) {
            d[i] += d[i - 1];
        }

        d[0] = 0;        

        /// build distinct value sequence
        int pos = 1;
        b[pos] = a[0];
        for (int i = 1; i < n; ++ i)
        {
            if (a[i] != a[i - 1]) {
                pos++;
                b[pos] = a[i];
            }
        }

        /// find value of result by binary search
        int lo = 0;
        int hi = a[n - 1] - a[0];
        int res = -1;
        while (lo <= hi) {
            int ss = 0;
            int mid = (lo + hi) >> 1;
            for (int i = 1; i < pos; ++ i) {
                if (b[i] + mid > 2e8) continue;
                /// calculate number of pairs that have difference less than or equal k by 
                /// calcuate number of numbers between a[i] and a[i] + tmp by using partial sum
                /// we will get the number of pairs by multiply the number of value between (a[i] and a[i] + tmp)
                /// and number of repeat a[i]
                /// calculate number of 0 by using sum Ex: {1, 1, 1, 1} => 3 * 4 / 2 = 6 (0s)
                ss += (tl[b[i]] * (tl[b[i]] - 1)) / 2;
                ss += (d[b[i] + mid] - d[b[i]]) * tl[b[i]];
            }
            ss += tl[b[pos]] * (tl[b[pos]] - 1) / 2;
            if (ss >= k) {
                res = mid;
                hi = mid - 1;
            }
            else lo = mid + 1;
        }
        return res;
    }
};
53659