-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathThree_Sum_Closest.py
More file actions
32 lines (28 loc) · 847 Bytes
/
Three_Sum_Closest.py
File metadata and controls
32 lines (28 loc) · 847 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
28
29
30
31
32
"""
@author - Anirudh Sharma
"""
from typing import List
def threeSumClosest(nums: List[int], target: int) -> int:
# Sort the given array
nums.sort()
# Length of the array
n = len(nums)
# Closest value
closest = nums[0] + nums[1] + nums[n - 1]
# Loop for each element of the array
for i in range(0, n - 2):
# Left and right pointers
j = i + 1
k = n - 1
# Loop for all other pairs
while j < k:
current_sum = nums[i] + nums[j] + nums[k]
if current_sum <= target:
j += 1
else:
k -= 1
if abs(closest - target) > abs(current_sum - target):
closest = current_sum
return closest
if __name__ == '__main__':
print(threeSumClosest([2, 3, 7, 1, 5, 7, 1, 8, -1, -4, -4, 3], 12))