package leetcode;

/**
 * https://leetcode.com/problems/k-diff-pairs-in-an-array/
 */
public class Problem532 {
    public int findPairs(int[] nums, int k) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem532 prob = new Problem532();
        System.out.println(prob.findPairs(new int[]{3, 1, 4, 1, 5}, 2)); // 2
        System.out.println(prob.findPairs(new int[]{1, 2, 3, 4, 5}, 1)); // 4
        System.out.println(prob.findPairs(new int[]{1, 3, 1, 5, 4}, 0)); // 1
    }
}

@@ -12,16 +12,113 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0A
 /**%0A * h
@@ -260,16 +260,25 @@
     
-// TODO%0A
+if (k %3C 0) %7B%0A    
     
@@ -295,337 +295,832 @@
  0;%0A
+    
     %7D%0A
-%0A
+ 
     
-public static void main(String%5B%5D args) %7B%0A        Problem532 prob = new Problem532();%0A        System.out.println(prob.findPairs(new int%5B%5D%7B3, 1, 4, 1, 5%7D, 2)); // 2%0A        System.out.println(prob.findPairs(new int%5B%5D%7B1, 2, 3, 4, 5%7D, 1)); // 4%0A        System.out.println(prob.findPairs(new int%5B%5D%7B1, 3, 1, 5, 4%7D, 0)); // 1
+   Map%3CInteger, Integer%3E map = new HashMap%3C%3E();%0A        for (int i = 0; i %3C nums.length; i++) %7B%0A            if (!map.containsKey(nums%5Bi%5D)) %7B%0A                map.put(nums%5Bi%5D, 1);%0A            %7D else %7B%0A                map.put(nums%5Bi%5D, map.get(nums%5Bi%5D) + 1);%0A            %7D%0A        %7D%0A        int result = 0;%0A        Set%3CString%3E set = new HashSet%3C%3E();%0A        for (int num : nums) %7B%0A            String key = Math.min(num, num + k) + %22%7C%22 + Math.max(num, num + k);%0A            if (!set.contains(key)) %7B%0A                int val = (num == num + k) ? 1 : 0;%0A                if (map.containsKey(num + k) && map.get(num + k) %3E val) %7B%0A                    map.put(num + k, map.get(num + k) - 1);%0A                    set.add(key);%0A                    result++;%0A                %7D%0A            %7D%0A        %7D%0A        return result;
 %0A   

Solve problem 532