This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
DSA/GA2/sum_of_three.py

39 lines
757 B
Python
Raw Normal View History

2019-03-24 15:40:10 +00:00
#!/usr/bin/env python3
import sys
def search_two(A, sum2, i_skip):
i = 0
j = len(A) - 1
while i < j:
if i == i_skip:
i = i + 1
elif j == i_skip:
j = j - 1
else:
cs = A[i] + A[j]
if cs == sum2:
return True
elif cs > sum2:
j = j - 1
else:
i = i + 1
return False
def sum_of_three(A, sum3):
A.sort() # assume using mergesort for worst case of O(n*log(n))
l = len(A)
for i in range(l):
if search_two(A, sum3 - A[i], i):
return True
return False
if __name__ == "__main__":
args = [int(x) for x in sys.argv[1:]]
print(sum_of_three(args[1:], args[0]))