-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairs.java
More file actions
50 lines (37 loc) · 1.28 KB
/
Copy pathpairs.java
File metadata and controls
50 lines (37 loc) · 1.28 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
import java.util.HashMap;
import java.util.Scanner;
public class Solution {
static int pairs(HashMap<Integer, Integer> allNumbers, int array[], int k) {
/* Complete this function */
int res = 0;
for (int key_in_array : array)
{
if (allNumbers.get(key_in_array+k) != null)
res++;
if (allNumbers.get(key_in_array-k) != null)
res++;
allNumbers.remove(key_in_array);
}
return res;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String n = in.nextLine();
String[] n_split = n.split(" ");
int _a_size = Integer.parseInt(n_split[0]);
int _k = Integer.parseInt(n_split[1]);
int[] _a = new int[_a_size];
int _a_item;
String next = in.nextLine();
String[] next_split = next.split(" ");
HashMap<Integer, Integer> allNumbers = new HashMap<>();
for(int _a_i = 0; _a_i < _a_size; _a_i++)
{
_a_item = Integer.parseInt(next_split[_a_i]);
_a[_a_i] = _a_item;
allNumbers.put(_a_item, 0);
}
System.out.println(pairs(allNumbers,_a, _k));
in.close();
}
}