✅3 Method || C++ || Beginner Friendly ||Beat 100%
To solve the problem of finding all unique pairs in an integer array where each pair has a specific difference \( k \), there are several approaches, each with 
To solve the problem of finding all unique pairs in an integer array where each pair has a specific difference \\( k \\), there are several approaches, each with its own complexity and trade-offs. Here\u2019s an overview of the approaches, including the best one:\n\n### 1. **Brute Force Approach**\n- **Idea**: Check all possible pairs in the array and count those that satisfy the condition.\n- **Implementation**:\n  ```cpp\n  int findPairs(vector<int>& nums, int k) {\n      int count = 0;\n      int n = nums.size();\n      for (int i = 0; i < n; i++) {\n          for (int j = i + 1; j < n; j++) {\n              if (nums[j] - nums[i] == k) {\n                  count++;\n              }\n          }\n      }\n      return count;\n  }\n  ```\n- **Complexity**: \n  - **Time Complexity**: \\( O(n^2) \\) (nested loops)\n  - **Space Complexity**: \\( O(1) \\)\n- **Drawback**: Inefficient for large arrays due to quadratic time complexity.\n---\n\n### 2. **Sorting + Two Pointers Approach**\n- **Idea**: Sort the array and use a two-pointer technique to find pairs with the desired difference.\n- **Implementation**:\n  ```cpp\n  int findPairs(vector<int>& nums, int k) {\n      sort(nums.begin(), nums.end());\n      int count = 0;\n      int n = nums.size();\n      int i = 0, j = 1;\n\n      while (i < n && j < n) {\n          if (i != j && nums[j] - nums[i] == k) {\n              count++;\n              i++;\n              j++;\n              // Skip duplicates\n              while (i < n && nums[i] == nums[i - 1]) i++;\n              while (j < n && j < n && nums[j] == nums[j - 1]) j++;\n          } else if (nums[j] - nums[i] < k) {\n              j++;\n          } else {\n              i++;\n              if (i == j) j++;\n          }\n      }\n      return count;\n  }\n  ```\n- **Complexity**: \n  - **Time Complexity**: \\( O(n log n) \\) (due to sorting) + \\( O(n) \\) (for the two-pointer traversal)\n  - **Space Complexity**: \\( O(1) \\)\n- **Pros**: More efficient than brute force; handles duplicates well.\n- **Cons**: Sorting the array may not be necessary for all applications.\n- ---\n\n### 3. **Hash Set Approach (Optimal Approach)**\n- **Idea**: Use a hash set to track the numbers and count unique pairs with a specific difference.\n- **Implementation**:\n  ```cpp\n  int findPairs(vector<int>& nums, int k) {\n      unordered_set<int> numSet;\n      unordered_set<int> seenPairs;\n      \n      for (int num : nums) {\n          numSet.insert(num);\n      }\n      \n      for (int num : numSet) {\n          if (numSet.count(num + k)) {\n              seenPairs.insert(num);\n          }\n      }\n      \n      return seenPairs.size();\n  }\n  ```\n- **Complexity**: \n  - **Time Complexity**: \\( O(n) \\)\n  - **Space Complexity**: \\( O(n) \\) (to store unique numbers)\n- **Pros**: Efficient and straightforward; avoids issues with sorting and is easy to implement.\n- **Cons**: Uses additional space for the hash set, though it is considered constant space relative to input.
['Array', 'Hash Table', 'Two Pointers', 'C++']