Advertisement
rajeshinternshala

Untitled

Aug 12th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.51 KB | None | 0 0
  1.  static int countPairsWithDiffK(List<Integer> list,
  2.                                    int k) {
  3.         int count = 0;
  4.         Collections.sort(list);
  5.         int n = list.size();
  6.         int l = 0;
  7.         int r = 0;
  8.         while (r < n) {
  9.             if (list.get(r) - list.get(l) == k) {
  10.                 count++;
  11.                 l++;
  12.                 r++;
  13.             } else if (list.get(r) - list.get(r) > k)
  14.                 l++;
  15.             else
  16.                 r++;
  17.         }
  18.         return count;
  19.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement