C++ solution | binary search | sorting | O(n)
\nint findPairs(vector<int>& nums, int k)\n{\n    sort(nums.begin(), nums.end());\n    int count = 0;\n    for (int i = 0; i < nums.size(); i++)\n    {\n       
```\nint findPairs(vector<int>& nums, int k)\n{\n    sort(nums.begin(), nums.end());\n    int count = 0;\n    for (int i = 0; i < nums.size(); i++)\n    {\n        if (i > 0 && i < nums.size() && nums[i - 1] == nums[i])\n        {\n            while (i < nums.size() && nums[i - 1] == nums[i])\n                i++;\n            if (i >= nums.size())\n                return count;\n        }\n        int find = k + nums[i];\n        int start = i + 1, end = nums.size() - 1;\n        while (start <= end)\n        {\n            int mid = start + (end - start) / 2;\n            if (nums[mid] == find)\n            {\n                count++;\n                break;\n            }\n            else if (find < nums[mid])\n                end = mid - 1;\n            else\n                start = mid + 1;\n        }\n\n    }\n    return count;\n}\n```
['Binary Search']