Skip to content

Instantly share code, notes, and snippets.

@wenweixu
Created September 2, 2019 23:22
Show Gist options
  • Save wenweixu/39b6c5d28dbfe7aae1f80d13e3106755 to your computer and use it in GitHub Desktop.
Save wenweixu/39b6c5d28dbfe7aae1f80d13e3106755 to your computer and use it in GitHub Desktop.
Pairs Hackerrank python solution 2: two pointers
def pairs(k, arr):
result = 0
arr = sorted(arr)
j = 1
for i in range(len(arr)-1):
while j<len(arr):
if arr[j] - arr[i] == k:
result += 1
j += 1
elif arr[j] - arr[i] > k:
break
elif arr[j] - arr[i] < k:
j += 1
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment