Brute Force to Functional Programming | Multiple Approaches
Solution 1: Brute Force Approach\n\n### Intuition\n- The simplest approach is to check all possible pairs in the array and see if their absolute difference equa
## Solution 1: Brute Force Approach\n\n### Intuition\n- The simplest approach is to check all possible pairs in the array and see if their absolute difference equals $$( k $$).\n\n### Approach\n- Use two nested loops to iterate through the array, check the absolute difference for each pair, and store unique pairs in a set to avoid duplicates.\n\n### Complexity\n- Time complexity: $$O(n^2)$$  \n- Space complexity: $$O(n)$$ (due to the set storing unique pairs)\n\n### Code\n```python3\nclass Solution:\n    def findPairs(self, nums: List[int], k: int) -> int:\n        n = len(nums)\n        numSet = set()\n\n        for i in range(n - 1):\n            for j in range(i + 1, n):\n                if abs(nums[i] - nums[j]) == k:\n                    numSet.add((nums[i], nums[j]))\n                    numSet.add((nums[j], nums[i]))\n\n        ans = len(numSet)\n        if k != 0:\n            ans //= 2\n\n        return ans\n```\n\n---\n\n## Solution 2: Two Pointers Approach\n\n### Intuition\n- By sorting the array, we can use two pointers to efficiently find pairs with the desired difference.\n\n### Approach\n- Sort the array and use two pointers to traverse it, adjusting the pointers based on the current difference.\n\n### Complexity\n- Time complexity: $$ O(n\\ log n) $$ (due to sorting)\n- Space complexity: $$ O(1) $$ (in-place sorting)\n\n### Code\n```python3\nclass Solution:\n    def findPairs(self, nums: List[int], k: int) -> int:\n        n = len(nums)\n        nums.sort()\n        ans = 0\n\n        i = 0\n        j = 1\n        while j < n:\n            if abs(nums[i] - nums[j]) < k:\n                j += 1\n            elif abs(nums[i] - nums[j]) > k:\n                i += 1\n            else:\n                ans += 1\n                i += 1\n                j += 1\n\n            while i > 0 and i < n and nums[i - 1] == nums[i]:\n                i += 1\n            while j > 0 and j < n and nums[j - 1] == nums[j]:\n                j += 1\n            if i == j:\n                j += 1\n\n        return ans\n```\n\n---\n\n## Solution 3: Using defaultdict\n\n### Intuition\n- Count the occurrences of each number and check for pairs based on the counts.\n\n### Approach\n- Utilize a `defaultdict` to store counts, then iterate through it to check for pairs.\n\n### Complexity\n- Time complexity: $$ O(n) $$\n- Space complexity: $$ O(m) $$ (where $$ m $$ is the number of unique elements)\n\n### Code\n```python3\nclass Solution:\n    def findPairs(self, nums: List[int], k: int) -> int:\n        d = defaultdict(int)\n\n        for num in nums:\n            d[num] += 1\n\n        ans = 0\n        for num in d:\n            pairExists = k > 0 and num + k in d\n            duplicateExists = k == 0 and d[num] > 1\n            if pairExists or duplicateExists:\n                ans += 1\n\n        return ans\n```\n\n---\n\n## Solution 4: Using lambda and reduce\n\n### Intuition\n- Leverage functional programming with `reduce` to compute the count of valid pairs.\n\n### Approach\n- Use `Counter` to count occurrences, then apply `reduce` to sum valid pairs based on conditions.\n\n### Complexity\n- Time complexity: $$ O(n) $$\n- Space complexity: $$ O(m) $$\n\n### Code\n```python3\nclass Solution:\n    def findPairs(self, nums: List[int], k: int) -> int:\n        d = Counter(nums)\n\n        return reduce(\n            lambda x, num: x + ((k > 0 and num + k in d) or (k == 0 and d[num] > 1)),\n            d,\n            0,\n        )\n```\n\n---\n\n## Solution 5: Using sum\n\n### Intuition\n- A concise way to count valid pairs using a generator expression with `sum`.\n\n### Approach\n- Use `Counter` to count occurrences, then use a generator expression to count valid pairs.\n\n### Complexity\n- Time complexity: $$ O(n) $$\n- Space complexity: $$ O(m) $$\n\n### Code\n```python3\nclass Solution:\n    def findPairs(self, nums: List[int], k: int) -> int:\n        d = Counter(nums)\n\n        return sum(\n            1 for num in d if (k > 0 and num + k in d) or (k == 0 and d[num] > 1)\n        )\n```
['Hash Table', 'Two Pointers', 'Sorting', 'Python3']