-
Notifications
You must be signed in to change notification settings - Fork 0
/
KdiffPairsinanArray.java
46 lines (38 loc) · 1.25 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
package arrays;
import java.util.*;
//https://leetcode.com/problems/k-diff-pairs-in-an-array/
//532. K-diff Pairs in an Array
public class KdiffPairsinanArray {
public static void main(String[] args) {
int nums[]={1,3,1,5,4};
KdiffPairsinanArray k= new KdiffPairsinanArray();
System.out.println(k.findPairs(nums,0));
}
public int findPairs(int[] nums, int k) {
int pair=0;
Set<Integer> vals= new HashSet<>();
for(int i=0;i<nums.length;i++){
boolean added=vals.add(nums[i]);
if(k==0 && !added)
pair++;
}
if(k==0 && vals.size()==1 && nums.length>1) return 1;
if(k==0) return pair;
//System.out.println(vals);
Iterator<Integer> it = vals.iterator();
Set<Integer> checked=new HashSet<>();
while(it.hasNext()){
int num=it.next();
if(vals.contains(num+k) && !checked.contains(num+k) ){
pair++;
checked.add(num);
checked.add(num+k);
}if(vals.contains(num-k) && !checked.contains(num-k) ){
pair++;
checked.add(num);
checked.add(num-k);
}
}
return pair;
}
}