Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created January 8, 2018 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/1ef3ce3f295febd2057822cfc66cd5ee to your computer and use it in GitHub Desktop.
Save jianminchen/1ef3ce3f295febd2057822cfc66cd5ee to your computer and use it in GitHub Desktop.
4 sum - two pointer technique - there are bugs
def find_array_quadruplet(arr, s):
#pass # your code goes here
arr.sort()
for i in range(len(arr)-3):
for j in range( i + 1,len(arr) - 2):
first = arr[i]
second = arr[j]
left = j + 1
right = len(arr) - 1
target = s - arr[i] - arr[j]
#value = arr[left] + arr[right] #
while left < right and arr[left] + arr[right] != target :
if arr[left] + arr[right] < target:
left += 1
else:
right -= 1
#if left >= right:
# break
#value = arr[left] + arr[right]
if arr[left] + arr[right] == target:
return [arr[i],arr[j],arr[left],arr[right]]
return []
# arr = [2, 7, 4, 0, 9, 5, 1, 3], s = 20
# 2, [7, 4, 0, 9, 5, 1, 3], 20 - 2 = 18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment