Java | Easy to understand |Two Approaches | Sorting | HashMap
Method 1:  using sorting and two pointers\n\nclass Solution {\n    public int findPairs(int[] nums, int k) {\n        Arrays.sort(nums);\n        int i=0, j=1, 
#### **Method 1:  using sorting and two pointers**\n```\nclass Solution {\n    public int findPairs(int[] nums, int k) {\n        Arrays.sort(nums);\n        int i=0, j=1, diff=0, n=nums.length, sum=Integer.MIN_VALUE;\n        int count=0;\n        while(j<n && i<n-1){\n\t\t    // ((nums[i]+nums[j])!=sum) -> this will take care of no repetetion\n\t\t\t//if we found any match, increase i , j by 1\n            if(nums[j]-nums[i]==k && (nums[i]+nums[j])!=sum){\n                sum=nums[i]+nums[j];\n                i++; j++; count++;\n            }\n\t\t\t//if diff is smaller than k increase j by 1\n\t\t\telse if((nums[j]-nums[i])<k){\n                j++;\n            }\n\t\t\t//else case, when diff is greater than k, increase i by 1\n\t\t\telse{\n                i++;\n            }\n\t\t\t//check if i and j are not same to aoid duplicates\n            if(i==j) j++;\n        }\n        return count;\n    }\n}\n```\n**Time Complexity** : O(nlogn) + O(n)\n**Space Complexity** : O(n)\n\n#### **Method 2 : using HashMap**\n```\nclass Solution {\n    public int findPairs(int[] nums, int k) {\n\t    // corner cases\n        if(nums==null || nums.length==0 || k<0) return 0;\n        int count=0;\n        Map<Integer, Integer> hash=new HashMap<>();\n\t\t//frequency map\n        for(int i:nums)\n            hash.put(i, hash.getOrDefault(i, 0)+1);\n        for(Map.Entry<Integer, Integer> entry:hash.entrySet())\n\t\t    // check if any such pair exist or not\n\t\t\t//in case of k==0 check whether any number having frequency >=2 or not.\n            if((hash.containsKey(entry.getKey()+k) && k!=0) || (k==0 && entry.getValue()>1))\n                count++;\n        return count;\n    }\n}\n```\n**Time Complexity** :  O(n)\n**Space Complexity** : O(n)\n\n**Don\'t forget to upvote, it inspires me a lot, Thank you!**
['Two Pointers', 'Sorting', 'Binary Tree', 'Java']