forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKdiffPairsinanArray.java
105 lines (80 loc) · 2.4 KB
/
KdiffPairsinanArray.java
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// @saorav21994
// TC: O(n)
// Medium
/*
Similar approach as sum of 2 nummber to a given sum in an array. Special case of (k == 0) -> check for frequency of the number >= 2 as only (x-x) will give 0.
*/
class Solution {
public int findPairs(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for (int n : nums) {
map.put(n, map.getOrDefault(n, 0) + 1);
}
int pairs = 0;
for (Integer key : map.keySet()) {
int mapVal = map.getOrDefault(key+k, 0);
if (k == 0) {
if (mapVal > 1)
pairs += 1;
}
else {
if (mapVal > 0)
pairs += 1;
}
}
return pairs;
}
}
// Problem : https://leetcode.com/problems/k-diff-pairs-in-an-array/
// @romitdutta10
// TC : O(n)
// Another Approach with two sets
class Solution {
public int findPairs(int[] nums, int k) {
if(nums == null || nums.length == 0) {
return 0;
}
Set<String> pairs = new HashSet<>();
Set<Integer> visited = new HashSet<>();
for(int num : nums) {
int greater = num + k;
int lesser = num - k;
if(visited.contains(greater)) {
pairs.add(num + " " + greater);
}
if(visited.contains(lesser)) {
pairs.add(lesser + " " + num);
}
visited.add(num);
}
return pairs.size();
}
}
// Problem : https://leetcode.com/problems/k-diff-pairs-in-an-array/
// @romitdutta10
// TC : O(nlogn)
// Solution with O(1) space
class Solution {
public int findPairs(int[] nums, int k) {
Arrays.sort(nums);
int left=0,right=1;
int ans=0;
while(left<nums.length && right<nums.length)
{
if(left==right || nums[right] - nums[left] < k)
right++;
else if(nums[right] - nums[left] > k)
left++;
else
{
left++;
ans++;
while(left<nums.length && nums[left] == nums[left-1])
{
left++;
}
}
}
return ans;
}
}