# 3 Sum Closest (Leetcode 16)

Problem Link: <https://leetcode.com/problems/3sum-closest/>

```python
class Solution:
    def threeSumClosest(self, nums: List[int], target: int) -> int:
        
        n=len(nums)
        nums.sort()
        minn=float('inf')
        ans=0
        summ=0
        for i in range(n-2):
            # If curr == prev, simply move forward
            if(1<=i<n-2 and nums[i]==nums[i-1]):
                i+=1
            
            j=i+1
            k=n-1
            
            while(j<k):
                summ=nums[i]+nums[j]+nums[k]
                if(summ==target):
                    return summ
                
                if(abs(summ-target)<minn):
                    minn=abs(summ-target)
                    ans=summ
                
                if(summ<target):
                    j+=1
                elif(summ>target):
                    k-=1
                
        return ans
```
