https://www.hackerrank.com/challenges/pairs/problem

1) 코드
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'pairs' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER k
# 2. INTEGER_ARRAY arr
#
def pairs(k, arr):
arrSet = set()
answerSet = set()
for num in arr:
arrSet.add(num)
for num in arr:
if num + k in arrSet:
answerSet.add(str(num) + str(num+k))
elif num - k in arrSet:
answerSet.add(str(num-k) + str(num))
return len(answerSet)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
k = int(first_multiple_input[1])
arr = list(map(int, input().rstrip().split()))
result = pairs(k, arr)
fptr.write(str(result) + '\n')
fptr.close()
2) 해설