3 Sum Closest || Leetcode Daily Challenge || Code + Intuition + Example
ฝัง
- เผยแพร่เมื่อ 20 ก.ย. 2024
- 16. 3Sum Closest
Medium
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example 1:
Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Link to challenge : leetcode.com/p...
the catch that at the second condition we only have to assign sum = arr[i]+arr[j]+arr[k], without doing any k-- or j++ had me going.
Thank you so much :)
Hey! how many problems have you solved on leetcode so far?
You made this problem so easy to understand. Thanxxx!!
Thankyou mam very nice and beautiful explanation can you please make video how to approch leetcode as begginer
U explain so nicely and concrete i jst love ur video please make more such video
//Same Explanation with different Solution
Arrays.sort(nums);
int n=nums.length;
int closeSum = nums[0]+ nums[1]+nums[n-1];
for(int i=0;i
why we are not skipping duplicate and calculating everytime for duplicate,we can use
// while (second < third and ar[second] == ar[second + 1]){
// second++;
// }
but this is not working Don't know why
please reply
We can use three pointers right for optimum solution
Nice and simpler explanation!
plg view full code after submitting and code typing time view max screen or possible to press f11 to full window.
Great Explanation
what if i store the elements one by one in new array of size three and by sorting them i can find the sum closest to target ?
Is this the best time complexity we can get?
somw time u r best
Thanks for this nice explanation
It would be much better if you could write a cleaner code, it is not just about getting the code submitted.
why we done minus target
to fing the difference, or to find which is closest ..to find the distance
why abs() is used? also why int start =num[i+1] rather than 0?please explain
abs is absolute value ...if diff is -ve it will consider in +ve only
amazing explanation
Thanks a lot maam :)
Please make video on suduko solver and valid sudoko 🙏🙏🙏🙏
Hey Shruti, thanks for asking, Sudoku solver video is already made:
th-cam.com/video/eLhmZlsGmvE/w-d-xo.html
ye kitti osm hai yaar 🥰
Tum n aise hi kisi ke bhi comment ko like mat karte jao,
Hame hope mil jati hai 🫣
@@rohitthakre215 😂
thanks a lot
nice
why diff = INT_MAX ?
to find the minimum between two values , one should be bigger , but for the first element , there is no second valuw to compare , so we take maximum integer possible .
@@shivamnegi7552 thank you vro
@@techieworld4269 love you
Good explanation. But your intendations are pretty weird !!!
difficult to understand from this
what is this test case failing can someone help?
test case - [1,1,1,0] , target =-100
my code->
int diff=INT_MAX;
for(int i=0;i