-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathKDiffPairs.java
More file actions
85 lines (71 loc) · 2.64 KB
/
KDiffPairs.java
File metadata and controls
85 lines (71 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
Problem:
Given an array of integers nums and an integer k, return the number of UNIQUE k-diff pairs in the array.
A k-diff pair is (a, b) such that |a - b| = k.
Key points:
- Pairs are unique by VALUE, not by index.
- If k < 0, answer is 0 (because absolute difference can't be negative).
Approach (HashMap frequency):
1) Build a frequency map: freq[x] = how many times x appears.
2) Iterate over each unique value "key" in the map:
- If k == 0:
We need pairs like (key, key). This is only valid if freq[key] >= 2.
- If k > 0:
We only count (key, key + k) to avoid double counting.
If (key + k) exists in the map, we count 1 unique pair.
3) Return count.
Why no double counting for k > 0?
- We only look in one direction: key -> key + k.
So (a, b) is counted once, not again as (b, a).
Time Complexity:
O(n) average time
Space Complexity:
- worst case O(n)
*/
import java.util.HashMap;
import java.util.Arrays;
public class KDiffPairs {
public int findPairs(int[] nums, int k) {
// Absolute difference cannot be negative
if (k < 0) return 0;
HashMap<Integer, Integer> freq = new HashMap<>();
for (int x : nums) {
freq.put(x, freq.getOrDefault(x, 0) + 1);
}
int count = 0;
for (int key : freq.keySet()) {
if (k == 0) {
// Need at least two occurrences to form (key, key)
if (freq.get(key) > 1) count++;
} else {
// Count pair (key, key + k) only once
int complement = key + k;
if (freq.containsKey(complement)) count++;
}
}
return count;
}
public static void main(String[] args) {
KDiffPairs solver = new KDiffPairs();
int[][] testArrays = {
{3, 1, 4, 1, 5}, // k=2 => 2
{1, 2, 3, 4, 5}, // k=1 => 4
{1, 3, 1, 5, 4}, // k=0 => 1
{1, 1, 1, 1}, // k=0 => 1
{1, 1, 2, 2}, // k=1 => 1
{-1, -2, -3}, // k=1 => 2
{1, 2, 3} // k=-1 => 0
};
int[] ks = {2, 1, 0, 0, 1, 1, -1};
int[] expected = {2, 4, 1, 1, 1, 2, 0};
for (int i = 0; i < testArrays.length; i++) {
int ans = solver.findPairs(testArrays[i], ks[i]);
System.out.println("Test " + (i + 1));
System.out.println("nums: " + Arrays.toString(testArrays[i]));
System.out.println("k: " + ks[i]);
System.out.println("output: " + ans);
System.out.println("expected: " + expected[i]);
System.out.println();
}
}
}