<p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>

<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>

<ul>
	<li><code>0 &lt;= i, j &lt; nums.length</code></li>
	<li><code>i != j</code></li>
	<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>

<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>-10<sup>7</sup> &lt;= nums[i] &lt;= 10<sup>7</sup></code></li>
	<li><code>0 &lt;= k &lt;= 10<sup>7</sup></code></li>
</ul>

void __print(int x) {cout << x;} void __print(long x) {cout << x;} void __print(long long x) {cout << x;} void __print(unsigned x) {cout << x;} void __print(unsigned long x) {cout << x;} void __print(unsigned long long x) {cout << x;} void __print(float x) {cout << x;} void __print(double x) {cout << x;} void __print(long double x) {cout << x;} void __print(char x) {cout << '\'' << x << '\'';} void __print(const char *x) {cout << '\"' << x << '\"';} void __print(const string &x) {cout << '\"' << x << '\"';} void __print(bool x) {cout << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}template<typename T> void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";} void _print() {cout << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
void db(const vector<bool>& v) { bool first = true; string res = "{"; for (int i = 0; i < int(v.size()); i++) { if (!first) res += ", "; first = false; res += to_string(v[i]); } res += "}"; cout<<res<<'\n';
}
class Solution {
public:
    int findPairs(vector<int>& a, int k) {
        int n = size(a);
        map<int, int> cnt;
        for(int x: a){
            cnt[x]++;
        }
        if(k == 0){
            int res = 0;
            for(auto [x, y]: cnt){
                if(y >= 2){
                    res++;
                }
            }
            return res;
        }
        int res = 0;
        vector<int> b;
        set<int> st;
        for(auto [x, y]: cnt){
            b.push_back(x);
            st.insert(x);        
        }
        for(int x: b){
            int need = x - k;
            if(st.count(need)){
                res++;
            }
            // |x - need| = k
            // x - need = k
            // need - x = k
            need = x + k;
            if(st.count(need)){
                res++;
            }
        }
        return res / 2;
    }
};
20700