-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathPairs.py
52 lines (42 loc) · 1.17 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
50
51
52
"""
Given N integers, count the number of pairs of integers whose difference is K.
Input Format
The 1st line contains N & K (integers).
The 2nd line contains N numbers of the set. All the N numbers are distinct.
Output Format
An integer that tells the number of pairs of integers whose difference is K.
Constraints:
N <= 10^5
0 < K < 10^9
Each integer will be greater than 0 and at least K smaller than 2^31-1
"""
__author__ = 'Danyang'
class Solution(object):
def solve(self, cipher):
"""
main solution function
:param cipher: the cipher
"""
N, K, lst = cipher
hm = {}
for val in lst:
if val in hm:
hm[val] += 1
else:
hm[val] = 1
cnt = 0
for val in lst:
target = val + K
if target in hm:
cnt += hm[target]
return cnt
if __name__ == "__main__":
import sys
f = open("1.in", "r")
# f = sys.stdin
N, K = map(int, f.readline().strip().split(' '))
lst = map(int, f.readline().strip().split(' '))
cipher = N, K, lst
# solve
s = "%s\n" % (Solution().solve(cipher))
print s,