Created
January 8, 2018 05:05
-
-
Save jianminchen/1ef3ce3f295febd2057822cfc66cd5ee to your computer and use it in GitHub Desktop.
4 sum - two pointer technique - there are bugs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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