프로그래밍/HackerRank
[HackerRank][Python3] Pairs
snoopybox
2018. 7. 17. 00:08
Problem :
https://www.hackerrank.com/challenges/pairs/problem
My Solution :
#!/usr/bin/env python3 def pairs(k, arr): found = 0 memo = {} for a in sorted(arr): if a-k in memo: found += 1 memo[a] = 1 return found n, k = map(int, input().split()) arr = list(map(int, input().rstrip().split())) result = pairs(k, arr) print(result)
My Solution2 :
#!/usr/bin/env python3 def pairs(k, arr): arr.sort() found = 0 i, j = 0, 1 while j < len(arr): if arr[j] - arr[i] == k: found += 1 j += 1 elif arr[j] - arr[i] < k: j += 1 else: i += 1 return found n, k = map(int, input().split()) arr = list(map(int, input().rstrip().split())) result = pairs(k, arr) print(result)