fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14. }
  15.  
  16. class Solution {
  17. public int countKDifference(int[] nums, int k) {
  18. HashMap<Integer , Integer> map = new HashMap<>();
  19. int n = nums.length;
  20. int pairs = 0;
  21. for(int i = 0 ; i< n ; i++){
  22. int curr = nums[i];
  23. int req1 = curr - k;
  24. int req2 = curr + k;
  25. int type1 = 1*map.getOrDefault(req1 , 0);
  26. int type2 = 1*map.getOrDefault(req2 , 0);
  27. pairs+=(type1 + type2);
  28. map.put(curr , map.getOrDefault(curr , 0) + 1);
  29. }
  30. return pairs;
  31. }
  32. }
Success #stdin #stdout 0.07s 54640KB
stdin
Standard input is empty
stdout
Standard output is empty