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.
defpairs(k,arr):# Convert the list to a set for O(1) lookupsarr_set=set(arr)count=0# Check for each element if there's a pair with difference kforxinarr:ifx+kinarr_set:count+=1ifx-kinarr_set:count+=1# Since each pair is counted twice, we return half the countreturncount// 2
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 →