532. K-diff Pairs in an Array. Cant understand why code fails the tests.


  • 0
    K
    1. K-diff Pairs in an Array. Cant understand why code fails the tests. How is expected 4?
      Input:
      [1,2,3,4,5]
      1
      Output:
      3
      Expected:
      4
      Here is my code:-
        public int findPairs(int nums[] , int k) {
            
            if(nums==null ||nums.length==0)
                return 0;
            HashSet<Integer> hs = new HashSet<Integer>();
            for(int i=0;i<nums.length;i++)
            {
                hs.add(nums[i]);
            }
            HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
            int count =0;
            for(Integer e: hs)
            {
                if(hm.containsKey(e))
                {
                    count++;
                }
    
                hm.put(e-2,e);
                hm.put(e+2,e);
    
            }
            return count;
        }
    }```

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.