Guest User

Untitled

a guest
Jan 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. def find_array_quadruplet(arr, s):
  2. #pass # your code goes here
  3. arr.sort()
  4. for i in range(len(arr)-3):
  5. for j in range( i + 1,len(arr) - 2):
  6. first = arr[i]
  7. second = arr[j]
  8.  
  9. left = j + 1
  10. right = len(arr) - 1
  11.  
  12. target = s - arr[i] - arr[j]
  13. #value = arr[left] + arr[right] #
  14.  
  15. while left < right and arr[left] + arr[right] != target :
  16. if arr[left] + arr[right] < target:
  17. left += 1
  18. else:
  19. right -= 1
  20.  
  21. #if left >= right:
  22. # break
  23. #value = arr[left] + arr[right]
  24.  
  25. if arr[left] + arr[right] == target:
  26. return [arr[i],arr[j],arr[left],arr[right]]
  27.  
  28. return []
  29.  
  30. # arr = [2, 7, 4, 0, 9, 5, 1, 3], s = 20
  31.  
  32. # 2, [7, 4, 0, 9, 5, 1, 3], 20 - 2 = 18
Add Comment
Please, Sign In to add comment