Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int countPairsWithDiffK(List<Integer> list,
- int k) {
- int count = 0;
- Collections.sort(list);
- int n = list.size();
- int l = 0;
- int r = 0;
- while (r < n) {
- if (list.get(r) - list.get(l) == k) {
- count++;
- l++;
- r++;
- } else if (list.get(r) - list.get(r) > k)
- l++;
- else
- r++;
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement