forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pascalsTriangle.txt
35 lines (35 loc) · 935 Bytes
/
pascalsTriangle.txt
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
33
34
35
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(),nums.end());
int diff=INT_MAX;
int ans=0;
for(int i=0;i<nums.size();i++)
{
int first=nums[i];
int start=i+1;
int end=nums.size()-1;
while(start<end)
{
if(first+nums[start]+nums[end]==target)
{
return target;
}
else if(abs(first+nums[start]+nums[end]-target)<diff)
{
diff=abs(first+nums[start]+nums[end]-target);
ans=first+nums[start]+nums[end];
}
if(first+nums[start]+nums[end]>target)
{
end--;
}
else
{
start++;
}
}
}
return ans;
}
};