Share this channel with anyone who needs genuine help with building strong logic. As a working professional, I regret not finding this channel earlier in my journey, but better late than never! With 3 years of experience, I still find it incredibly valuable. Thanks, MIK!
This is by far the best solution i have seen on You tube Thank you for explaining Medium level ka question lga hi nhi aapke explanation k wjh se thank you Highly Appreciate your efforts
Thank you bhaiya for explaining the approach so deeply. I have one question regarding the input array below: -4, -1, -1, 0, 0, 1, 2, 2 In this in the first step we assume that the n1 is -4 and -n1 will be -(-4) or 4. Now we need to find number n2 and n3 which adds up to 4. Starting from i at 1 and j at 7, we first increment the i as value at i + 1 is same as value at i and similarly we decrement the j as value at j - 1 is equal to value at j. After that we continue wit this approach and updating the variables i and j until they cross. My question is that {-4, 2, 2} can be one of the elements of the output array. I mean it clearly is as the triplet sum is 0, but following the approach for this as briefly discussed above, value at i will never reach the value 2 in this case. Could you or anyone please explain if I missed something here?
bhayia ek baat batayie 18:40 mein aapne 2 2 times kyu considerd kiya apne apke logic ke hissab se 2 to skip ho jana chaiye the because j == j -1. Answer {-4, 2,2} kasie?
For those who are having same doubt : This doubt got cleared through code. we will increment - decrement i and j based on their duplicacy only if i th + j th element provides us the target element. as it was the root of why increment/decrement coz we were getting answer and if not skipped we will again get same answers again. hence even in (0,0,0) example we get the expected answer.
Hi @adarshsrivastava3657, ***Let me share my long observation.*** I also had the same Qn when I started following this channel. I noticed that this guy never ever asked or forced to subscribe in any of his video. Other UA-camrs almost mention this in their every video but this guy is unique. May be that's why he is not famous till now. I once shared his channel to two of my friends in college and now whole college (CS & IT) knows this guy as MIK and everyone literally watches his channel. In one of his videos, he also mentioned he rejected some offers from some Paid Platform (he didn't mention the company name) to advertise their platform in his videos. This caught my attention on how different mik is. I was a big fan of striver and love Babbar but even they also promote paid platforms which I don't think are good enough and only charge fees to sell their course etc. This is what I found from his GitHub repository - "Why I am doing this ? Because knowledge should be free. You don't need to pay for courses. Learn free, share free. PEACE 💓" The respect for this guy increased more and more with time and I became a big fan of this legend. People might ask why I have written such a long reply to this comment - My answer is because generally we don't see our unsung heroes and in this case, it's this legend MIK who is doing everything for free.
Hello , can someone solve my doubt how we will get {-4 ,2 , 2 } as answer because if(arr [ j ] = = arr [ j - 1 ] ) we do j - - . So how we can get 2 + 2 , and also i < j . Can someone help me?
Hey! Awesome explanation as always, btw I was wondering why did you use begin(nums) rather than nums.begin(), I was coding the algorithm by myself, I used nums.begin(), now I had to push to the vector before the repetition checks in the two sum function, I googled it, but didn't get any sure answers from SO too. Any Idea?
Hi Ausaf. Both are same. begin(nums) nums.begin() I just usually use both of them interchangeably just for my practice and knowledge 🙂 Hope that answers your qns. Is there any other doubt that I couldn’t clear ? Kindly let me know. Thanks for watching ❤️❤️❤️
The way you pointed out why we need to see if n1 is repeated (considered already) and why same logic will apply in repeating i and j when finding n2,n3 is 🤌🏻
bhaiya will you please tell me why we are looking for n2 and n3 in the arr after n1, because it might happen that n2 or n3 can lie before n1 (fixed) also.
Java Solution below: package week7.day43; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ThreeSum { public static void main(String[] args) { int[] nums = {-4, -2, 1, -5, -4, -4, 4, -2, 0, 4, 0, -2, 3, 1, -5, 0}; List lists = threeSum(nums); System.out.println(lists); } private static List threeSum(int[] nums) { int n = nums.length; List list = new ArrayList(); int target = Integer.MAX_VALUE; if (n < 3) return list; Arrays.sort(nums); for (int x = 0; x < n; x++) { if (target == -(nums[x])) continue; target = -(nums[x]); int i = x + 1; int j = n - 1; while (i < j) { List l = new ArrayList(); int sum = nums[i] + nums[j]; if (sum > target) j--; else if (sum < target) { i++; } else { while (i < j && nums[i] == nums[i + 1]) i++; while (i < j && nums[j] == nums[j - 1]) j--; list.add(Arrays.asList(nums[x], nums[i], nums[j])); i++; j--; } } } return list; } }
Wanted to see your approach to this problem...literally the best solution for the 3-Sum problem present on UA-cam!
Kudos!
You made my day ❤️
Thank you so much 😇😇
Kuch aur search kiya tha kuch aur aagaya but dekhne ke baad ye bhi intresting lag raha hai.
Share this channel with anyone who needs genuine help with building strong logic. As a working professional, I regret not finding this channel earlier in my journey, but better late than never! With 3 years of experience, I still find it incredibly valuable. Thanks, MIK!
I'm glad I found you during my DSA journey. Thanks for everything!
This is by far the best solution i have seen on You tube Thank you for explaining Medium level ka question lga hi nhi aapke explanation k wjh se thank you Highly Appreciate your efforts
Thank you so much Shashank ❤️❤️❤️
3 sum is ❤️. Got to learn a lot from it.
Thats what she said
Hello mik sir, you r helping me to understand dsa concepts, thank you so much sir, god bless you❤
Bhaiya apke itne ache explanation ki wajah se 4SUM wala khud kar diya maine
Thank you bhaiya🥰
ab ga**b** baaki hai bass, woh bhi karlo
Thank you bhaiya for explaining the approach so deeply. I have one question regarding the input array below:
-4, -1, -1, 0, 0, 1, 2, 2
In this in the first step we assume that the n1 is -4 and -n1 will be -(-4) or 4. Now we need to find number n2 and n3 which adds up to 4.
Starting from i at 1 and j at 7, we first increment the i as value at i + 1 is same as value at i and similarly we decrement the j as value at j - 1 is equal to value at j.
After that we continue wit this approach and updating the variables i and j until they cross. My question is that {-4, 2, 2} can be one of the elements of the output array. I mean it clearly is as the triplet sum is 0, but following the approach for this as briefly discussed above, value at i will never reach the value 2 in this case.
Could you or anyone please explain if I missed something here?
seriously i got so much help because of you to how to think for code and urs approaches are very awesome thanks for giving me such type of knowledge
Thank you ❤️😇
Was not able to solve and understand intuitions of solutions available already Today and Felt very low .
Hands down to best solution 🙏
This was the best approach for this problem. Thanks for this explanation! Such a great channel.
You're very welcome! ❤️😇🙏
Bhai !! what an explaination ! fantastic
Assalamualaikum Brother,
Dil se Salam hai aise Explaination aur Explain karne wale pe ❤
Walaikum Assalam Bari.
Thank you so much for your precious comment ❤️❤️❤️
sir , i don't have words to say thankyou for all the videos you have uploaded , you are really a gem trust me
Greatest explanation ❤🔥
thank you bhaiii ,thank you soo much for your all efforts
The way u explain each question feels like every dsa question is just a cupcake 😭😭
really.. u r too good sir.. hats off
👌great stuff
Bro you solved my problem.Thanks
You point all the valuable , plese make more playlist ,you are op
Just Awesome.
Amazing Explanation💫
Thanks a lot Zaviya
u don't make it just easy U make us also believe on it
Thanks a lot bhaiya ❤❤
nice i dont usually comment but hats off
bhaiiii mn maan gae hu apko , APKO GOOGLE KA CEO HONA CHAHYE
bhayia ek baat batayie 18:40 mein aapne 2 2 times kyu considerd kiya apne
apke logic ke hissab se 2 to skip ho jana chaiye the because j == j -1.
Answer {-4, 2,2} kasie?
For those who are having same doubt :
This doubt got cleared through code.
we will increment - decrement i and j based on their duplicacy only if i th + j th element provides us the target element.
as it was the root of why increment/decrement coz we were getting answer and if not skipped we will again get same answers again.
hence even in (0,0,0) example we get the expected answer.
while fixing a particular number why dont we consider the umber on left side
please upload the solution video of find triplets of zero sum
Understood
underrated playlist hai
why only 12.3K sub..??
Hi @adarshsrivastava3657,
***Let me share my long observation.***
I also had the same Qn when I started following this channel. I noticed that this guy never ever asked or forced to subscribe in any of his video. Other UA-camrs almost mention this in their every video but this guy is unique. May be that's why he is not famous till now. I once shared his channel to two of my friends in college and now whole college (CS & IT) knows this guy as MIK and everyone literally watches his channel.
In one of his videos, he also mentioned he rejected some offers from some Paid Platform (he didn't mention the company name) to advertise their platform in his videos.
This caught my attention on how different mik is. I was a big fan of striver and love Babbar but even they also promote paid platforms which I don't think are good enough and only charge fees to sell their course etc. This is what I found from his GitHub repository - "Why I am doing this ? Because knowledge should be free. You don't need to pay for courses. Learn free, share free. PEACE 💓"
The respect for this guy increased more and more with time and I became a big fan of this legend.
People might ask why I have written such a long reply to this comment - My answer is because generally we don't see our unsung heroes and in this case, it's this legend MIK who is doing everything for free.
Literally Mai bhi itna acha ye samjane ke baad deko to he deserves more
55.7K now
58.4K now
74.5K now
Awsm video🤓
Can you please tell the time complexity?
sei sei
12:24
Gem ❤
Hello , can someone solve my doubt how we will get {-4 ,2 , 2 } as answer because if(arr [ j ] = = arr [ j - 1 ] ) we do j - - . So how we can get 2 + 2 , and also i < j . Can someone help me?
Bhai, ap konsa software use karthe hoo, drawing keliye
if the array is not sorted , then how to remove duplicates in this case?
Please also add tc & sc on it
Hey! Awesome explanation as always, btw I was wondering why did you use begin(nums) rather than nums.begin(), I was coding the algorithm by myself, I used nums.begin(), now I had to push to the vector before the repetition checks in the two sum function, I googled it, but didn't get any sure answers from SO too. Any Idea?
Hi Ausaf.
Both are same.
begin(nums)
nums.begin()
I just usually use both of them interchangeably just for my practice and knowledge 🙂
Hope that answers your qns.
Is there any other doubt that I couldn’t clear ?
Kindly let me know.
Thanks for watching ❤️❤️❤️
@@codestorywithMIK ohk, the explanation was on point as always. 😊
Why was result array cleared 🤔 @29:39
Bhai clear nahi karoge to bhi chalega it still works.
Wahi mai bhi soch rha tha bhai
The way you pointed out why we need to see if n1 is repeated (considered already) and why same logic will apply in repeating i and j when finding n2,n3 is 🤌🏻
bhaiya yha pe to smjh aa jata hai interview mein yd hi ni rhta
bhaiya will you please tell me why we are looking for n2 and n3 in the arr after n1, because it might happen that n2 or n3 can lie before n1 (fixed) also.
He told us that first element will be n1....so how can n2 and n3 lie before n1
Day -1
why did u cleared the result sir?
\
why dont we use hashset for duplication?
No use of extra space already mentioned
@@ankitankit3391 Hmmm,Thanks
java version please?actually in java some test cases failed from my side
Java solution please
Java Solution below:
package week7.day43;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ThreeSum {
public static void main(String[] args) {
int[] nums = {-4, -2, 1, -5, -4, -4, 4, -2, 0, 4, 0, -2, 3, 1, -5, 0};
List lists = threeSum(nums);
System.out.println(lists);
}
private static List threeSum(int[] nums) {
int n = nums.length;
List list = new ArrayList();
int target = Integer.MAX_VALUE;
if (n < 3)
return list;
Arrays.sort(nums);
for (int x = 0; x < n; x++) {
if (target == -(nums[x]))
continue;
target = -(nums[x]);
int i = x + 1;
int j = n - 1;
while (i < j) {
List l = new ArrayList();
int sum = nums[i] + nums[j];
if (sum > target)
j--;
else if (sum < target) {
i++;
} else {
while (i < j && nums[i] == nums[i + 1])
i++;
while (i < j && nums[j] == nums[j - 1])
j--;
list.add(Arrays.asList(nums[x], nums[i], nums[j]));
i++;
j--;
}
}
}
return list;
}
}
Bhaiya abhi ek naya edge case add hua hai - input is [0,0,0]. Isme problem ye ho rhi hai ki because all elements are same sab kuch skip ho ja rha.
Hey,
No it won’t be skipped .
The expected output is {{0,0,0}}
The code will give this output.
@@codestorywithMIK Yes I figured that out later. Thanks !
legit