We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
from collections import defaultdict
arr_elements=set(arr)
hmap=defaultdict(int)
for i in range(len(arr)):
if arr[i]+k in arr_elements and (arr[i], arr[i]+k) not in hmap:
hmap[(arr[i], arr[i]+k)]+=1
if arr[i]-k in arr_elements and (arr[i]-k, arr[i]) not in hmap:
hmap[(arr[i]-k, arr[i])]+=1
return sum(hmap.values())
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Pairs
You are viewing a single comment's thread. Return to all comments →
Python3 Optimal Solution:
def pairs(k, arr):