532: Solution with step by step explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. First, check if k is less than 0, if yes then return 0.\n2. Create a h
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. First, check if k is less than 0, if yes then return 0.\n2. Create a hash table freq to store the frequency of each number in the array.\n3. Iterate through the array nums and for each number, update its frequency in the hash table freq.\n4. Initialize a variable count to 0 to store the count of unique pairs.\n5. Iterate through the keys in the hash table freq and check for each key if there exists a pair with difference k in the hash table.\n6. If k is 0, then increment the count only if the frequency of the number is greater than 1.\n7. If k is not 0, then check if num + k exists in the hash table. If yes, then increment the count.\n8. Return the count of unique pairs.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n    def findPairs(self, nums: List[int], k: int) -> int:\n        if k < 0:\n            return 0\n        \n        # initialize hash table\n        freq = {}\n        for num in nums:\n            freq[num] = freq.get(num, 0) + 1\n        \n        # iterate through array and find pairs\n        count = 0\n        for num in freq:\n            if k == 0:\n                if freq[num] > 1:\n                    count += 1\n            else:\n                if num + k in freq:\n                    count += 1\n        \n        # return count of unique pairs\n        return count\n\n```
['Array', 'Hash Table', 'Two Pointers', 'Python', 'Python3']