forked from netsetos/python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sum
More file actions
27 lines (26 loc) · 603 Bytes
/
3sum
File metadata and controls
27 lines (26 loc) · 603 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def threeSum(arr, target):
arr.sort()
size=len(arr)
max=float('inf')
res=0
i=0
while i<size:
start = i + 1
end= size -1
while start < end:
sum = arr[i] + arr[start] + arr[end]
diff = abs(sum - target)
if diff == 0:
return sum
if diff < max:
max = diff
res = sum
if sum <= target:
start += 1
else:
end -= 1
i=i+1
return res
arr= [7,12,3,1,2,-6,5,-8,6]
target = 0
print(threeSum(arr,target))