532. K-diff Pairs in an Array

Description:

https://leetcode.com/problems/k-diff-pairs-in-an-array/description

Code:

Time & Space:
O(nlog(n)) & O(n)

One thought on “532. K-diff Pairs in an Array”

  1. The O(N) version:

    class Solution {
    public:
    int findPairs(vector& nums, int k) {
    if (k < 0) return 0;
    unordered_set res, vd;
    for (const int x : nums) {
    if (vd.count(x – k)) {
    res.insert(x – k);
    }
    if (vd.count(x + k)) {
    res.insert(x);
    }
    vd.insert(x);
    }
    return res.size();
    }
    };

Leave a Reply

Your email address will not be published. Required fields are marked *