You are viewing a single comment's thread. Return to all comments →
public static int pairs(int k, List<Integer> a) { List<Integer> arr = new ArrayList<>(a); Collections.sort(arr); int leftptr = 0; int rightptr = 1; int pairs = 0; while(rightptr>leftptr && rightptr<arr.size()) { int diff = arr.get(rightptr)-arr.get(leftptr); if(diff == k) { pairs++; rightptr++; } else if(diff > k) { if(rightptr-leftptr > 1) leftptr++; else if(rightptr-leftptr == 1) { leftptr++; rightptr++; } } else { rightptr++; } } return pairs; }
Seems like cookies are disabled on this browser, please enable them to open this website
Pairs
You are viewing a single comment's thread. Return to all comments →