Self-explained AC Java Sliding Window
\n public  int findPairs(int[] nums, int k) {\n\tif(k<0 || nums.length<=1){\n\t    return 0;\n\t}\n\t\t \n         Arrays.sort(nums);\n         int count = 0;\n
```\n public  int findPairs(int[] nums, int k) {\n\tif(k<0 || nums.length<=1){\n\t    return 0;\n\t}\n\t\t \n         Arrays.sort(nums);\n         int count = 0;\n         int left = 0;\n         int right = 1;\n         \n         while(right<nums.length){\n             int firNum = nums[left];\n             int secNum = nums[right];\n             // If less than k, increase the right index\n             if(secNum-firNum<k){\n                 right++;\n             }\n             // If larger than k, increase the left index\n             else if(secNum - firNum>k){\n                 left++;   \n             }\n             // If equal, move left and right to next different number\n             else{\n                 count++;\n                 while(left<nums.length && nums[left]==firNum){\n                     left++;\n                 }\n                 while(right<nums.length && nums[right]==secNum){\n                     right++;\n                 }\n                             \n             }\n             //left and right should not be the same number\n             if(right==left){\n             \tright++;\n             }\n         }\n        return count;\n    }\n```
[]