-
Notifications
You must be signed in to change notification settings - Fork 2
/
pairs.py
49 lines (44 loc) · 1.01 KB
/
pairs.py
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
import os
numbers = []
def sort(l, r):
if (l >= r):
return
t = numbers[l]
i = l
j = r
while (i < j):
while (i < j and numbers[j] >= t):
j -= 1
if (i < j):
numbers[i] = numbers[j]
i += 1
while (i < j and numbers[i] <= t):
i += 1
if (i < j):
numbers[j] = numbers[i]
j -= 1
numbers[i] = t
sort(l, i - 1)
sort(i + 1, r)
def main():
n, k = raw_input().split()
n = int(n)
k = int(k)
for i in raw_input().split():
numbers.append(int(i))
sort(0, n - 1)
res = 0
j = 1
for i in range(n - 1):
if (numbers[n - 1] - numbers[i] < k):
break
while (j < n):
if (numbers[j] - numbers[i] >= k):
break
j += 1
if (numbers[j] - numbers[i] == k):
res += 1
j += 1
print res
if __name__ == '__main__':
main()