-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairs_hackerrank.java
More file actions
70 lines (58 loc) · 1.91 KB
/
Copy pathpairs_hackerrank.java
File metadata and controls
70 lines (58 loc) · 1.91 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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int pairs(int[] a,int k) {
if(a.length == 0)
return 0;
int res=0;
HashMap<Integer,Boolean> hm = new HashMap<Integer,Boolean>();
HashMap<String,Boolean> ch = new HashMap<String,Boolean>();
for(int i=0;i<a.length;i++)
{
hm.put(a[i],true);
}
for(int i=0;i<a.length;i++)
{
if(hm.containsKey(Math.abs(a[i]-k)) && a[i]!=Math.abs(a[i]-k) )
{
String val = Integer.toString(a[i])+","+Integer.toString(a[i]-k);
String val1 = Integer.toString(a[i]-k)+","+Integer.toString(a[i]);
//System.out.println(val1+val);
if(ch.containsKey(val) || ch.containsKey(val1))
{
continue;
}
else
{
ch.put(val,true);
ch.put(val1,true);
++res;
}
// hm.remove(a[i]);
}
}
return res;
//return 0;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int res;
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(" ");
for(int _a_i = 0; _a_i < _a_size; _a_i++) {
_a_item = Integer.parseInt(next_split[_a_i]);
_a[_a_i] = _a_item;
}
res = pairs(_a,_k);
System.out.println(res);
}
}