Two-pointer Approach
The problem is just a variant of 2-sum.\nUpdate: Fixed a bug that can cause integer subtraction overflow.\nUpdate: The code runs in O(n log n) time, using O(1) 
The problem is just a variant of 2-sum.\n**Update:** Fixed a bug that can cause integer subtraction overflow.\n**Update:** The code runs in `O(n log n)` time, using `O(1)` space.\n\n```java\npublic int findPairs(int[] nums, int k) {\n    int ans = 0;\n    Arrays.sort(nums);\n    for (int i = 0, j = 0; i < nums.length; i++) {\n        for (j = Math.max(j, i + 1); j < nums.length && (long) nums[j] - nums[i] < k; j++) ;\n        if (j < nums.length && (long) nums[j] - nums[i] == k) ans++;\n        while (i + 1 < nums.length && nums[i] == nums[i + 1]) i++;\n    }\n    return ans;\n}\n```
[]