Java two pointer solution beats 97%
The idea is simple. Sort the array first. Then for each number in the array, find if there exists a number satisfy the requirement. Note that right = Math.max(r
The idea is simple. Sort the array first. Then for each number in the array, find if there exists a number satisfy the requirement. Note that ```right = Math.max(right, i + 1)``` can make sure each number in the array is accessed at most twice. So the time complexity is O(nlogn) + O(n) = O(nlogn)\n```\npublic int findPairs(int[] nums, int k) {\n    if (nums.length < 2 || k < 0) {\n        return 0;\n    }\n    int count = 0;\n    Arrays.sort(nums);\n    int right = 0;\n    for (int i = 0; i < nums.length; i++) {\n        if (i > 0 && nums[i] == nums[i - 1]) {\n            continue;\n        }\n            \n        right = Math.max(right, i + 1);\n        while (right < nums.length) {\n            if (nums[right] - k == nums[i]) {\n                count++;\n                break;\n            } else if (nums[right] - k < nums[i]) {\n                right++;\n            } else {\n                break;\n            }\n        }\n    }\n    return count;\n}\n```
[]