-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairs.java
More file actions
31 lines (23 loc) · 707 Bytes
/
Copy pathPairs.java
File metadata and controls
31 lines (23 loc) · 707 Bytes
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
//Given integers, count the number of pairs of integers whose difference is .
//The first line contains array and the number(difference).
//Returns An integer that tells the number of pairs of integers whose difference is K.
package HackerRank;
import java.util.HashSet;
import java.util.Set;
public class Pairs {
public static int pairs(int[] a, int k){
Set<Integer> set = new HashSet<Integer>();
int count = 0;
for(int i : a){
if(set.contains(i + k)) count++;
if(set.contains(i - k)) count++;
set.add(i);
}
return count;
}
public static void main(String args[]){
int a[] = {1,5,3,4,2};
int k = 2;
System.out.println(pairs(a,k));
}
}