-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindAllPairs.java
82 lines (70 loc) · 2.12 KB
/
FindAllPairs.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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Problem1 {
private static int[] num;
public static void main(String[] args) {
int n = 0, k = 0;
String line = "";
int lineNum = 0;
int i = 0;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
int tokenNum = 0;
while(st.hasMoreTokens()) {
String token = st.nextToken();
if(lineNum == 0) {
if(tokenNum == 0) {
n = Integer.parseInt(token);
num = new int[n];
}
if(tokenNum == 1)
k = Integer.parseInt(token);
tokenNum++;
}
else {
num[i] = Integer.parseInt(token);
i++;
}
}
lineNum++;
}
int pairs = findAllPairs(num, k);
System.out.println("Number of pairs: " + pairs);
}
catch(IOException ex) {
System.out.println(ex);
}
}
private static int findAllPairs(int[] num, int k) {
// Sort
Arrays.sort(num);
int len = num.length;
int low = 0, high = len-1;
int pairs = 0;
for(int i = 0; i < len; i++) {
int target = num[i] + k;
low = i;
while(low != high) {
int mid = (low + high)/2;
if(target < num[mid]) {
high = mid - 1;
}
else if(target == num[mid]) {
pairs++;
break;
}
else
low = mid + 1;
}
if(low == high)
if(target == num[low])
pairs++;
}
return pairs;
}
}