-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpairs.py
More file actions
39 lines (18 loc) · 724 Bytes
/
pairs.py
File metadata and controls
39 lines (18 loc) · 724 Bytes
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
# Solution of the challenge Pairs proposed on HackerRank at https://www.hackerrank.com/challenges/pairs/problem
import math
import random
import re
import sys
# Given the array arr, we should add the target value k to each of its elements, so that we get a new array called
# sum_set. After converting both arr and sum_set to sets, the answer is the length of the intersection between
# the two sets.
def pairs(k, arr):
sum_set = set(item + k for item in arr)
return len(set.intersection(set(arr), sum_set))
if __name__ == '__main__':
nk = input().split()
n = int(nk[0])
k = int(nk[1])
arr = list(map(int, input().rstrip().split()))
result = pairs(k, arr)
print(result)