Please watch our new video on the same topic: kzbin.info/www/bejne/m3WcZoqIm81rZ8U
@uniqueyuvrajgaming339210 ай бұрын
You said it is O(n3) but sorting will take some time to what about that?
@janardhan2jordan9 ай бұрын
@@uniqueyuvrajgaming3392nlog n is basically negligible in comparision of n3 thats why is it not included
@TaimoorKhan-h6l5 ай бұрын
Sir,I have one confusion about the time complexity. Inside the innermost loop we have the insert operation in set which take O(logN) in the worst case considering we have n elements inside the side. If we are taking the worst case means that the if(sum==target) is true for each condition.Then Why we are not considering it's complexity. According to my calculation,Time Complexity is O(N^3(logN)). Please sir reply. Thank you in Advance.
@TaimoorKhan-h6l5 ай бұрын
@@uniqueyuvrajgaming3392 Sorting will have always 4 elements which will take constant time.
@mohitshahu46012 ай бұрын
@@TaimoorKhan-h6l do you want to use ordered set?
@shailesh_rajpurohit Жыл бұрын
Note: 0:42 Make sure you watch the 3Sum before doing the 4Sum . 🙂
@TrashAccount-ye3ul Жыл бұрын
What if I have seen since a long ago, will it work ?🙃 But why he was laughing while saying this ?
@nikhilKumar-xv4mt Жыл бұрын
😂😂
@kanhiyasharma364810 ай бұрын
naughty audience😂😂😂😂
@MandeepSingh-rr8kk6 ай бұрын
ok 🙂
@ashishashish14035 ай бұрын
Whole video is about 4 sum but 0:40 to 0:50 is wholesome
@yashsharma6396 Жыл бұрын
Excited to do 4sum after completing 3sum yesterday
@gareer2436 Жыл бұрын
Damn bro include me next time
@yashsharma6396 Жыл бұрын
@@gareer2436 you know how to bend?😂😂
@gregorirasputin659 Жыл бұрын
Yaar tum log bohat tharki ho 😂
@gareer2436 Жыл бұрын
@@yashsharma6396 no but I know what to do when someone bends...😏 Lay them down and.. Massage their backs of course!
@gareer2436 Жыл бұрын
@@gregorirasputin659 ma to sath me problem solve krne ki baat kr raha tha bro... tum kya soch liye?😏
@Akash-yr2if Жыл бұрын
0:44 Striver's Smile Says it all
@brijmohan_f_s Жыл бұрын
you got my point bro😁
@AkOp-bf9vm6 ай бұрын
bhai bhai🫡🫡😂. what a detailing bro
@guneeshvats46Ай бұрын
Damn I was about to comment this
@SIVAKUMAR-bj2vlАй бұрын
Hhahahahahahaha
@jatinsareen7771 Жыл бұрын
3 sum was awesome, 4 sum was fantastic!!!!
@suyashrahatekar4964 Жыл бұрын
legends like 5 sum
@Akash-yr2ifАй бұрын
Ultra Legends like 6 sum
@takeUforward Жыл бұрын
Do give us a like and subscribe, it won't cost you anything, but it will highly motivate us to create more such type of videos :)
@rishav144 Жыл бұрын
striver ..king of DSA
@shra1 Жыл бұрын
The if condition inside while to check if(k
@codemania2878 Жыл бұрын
sir which whiteboard pen app you use for explain the code
@itsmrcrazy7670 Жыл бұрын
hey striver you forgot to add time complexity required for --sorting array --optimal approach so it will be O(NlogN) + O(N^2). plz update
@BhaveshKumar-z1h Жыл бұрын
@@codemania2878 He is using an iPad and its default Notes app
@shubhamagarwal14343 ай бұрын
#Free Education For All.. # Bhishma Pitamah of DSA...You could have earned in lacs by putting it as paid couses on udamey or any other elaerning portals, but you decided to make it free...it requires a greate sacrifice and a feeling of giving back to community, there might be very few peope in world who does this...."विद्या का दान ही सर्वोत्तम दान होता है" Hats Off to you man, Salute from 10+ yrs exp guy from BLR, India.
@arujgarg7267 Жыл бұрын
Finally did 4 sum, amazing experience
@gareer2436 Жыл бұрын
Can you elaborate your experience? Asking for a friend
@catalyst8654 Жыл бұрын
can you explain about your experience...🙂🙂
@leisuretime4198 Жыл бұрын
@@catalyst8654 🤣🤣
@Akash-yr2if Жыл бұрын
Would you take a moment to share your experience with us.
@cinvest2411 Жыл бұрын
You should not take I upto
@SasukeUchiha-pl8leАй бұрын
it doesn't create a big difference
@selvaarumugam370 Жыл бұрын
the way you explain the two pointer approaches and the use of them makes two pointer approach the easiest way to solve a problem. I fell in love the way you are teaching these concepts.
You watch all the videos? I see you daily updating me with timestamps, massive thanks for that.
@mehulthuletiya497 Жыл бұрын
@@takeUforward Yes, I watch the all the videos. After the watching video i comment the Timestamp. Recursion and Backtracking series is the one of the best explanation and understanding with your teaching style. I completed 2 days ago.
@salihedneer8975 Жыл бұрын
@@takeUforward striver please update timestamp in disucssion so that ppl can access it easily
@dishankbaid05 Жыл бұрын
The consistency level of striver is really the source of motivation 🔥.
@impalash_ag4 ай бұрын
Hi Raj, there are 2 slight mistakes in your optimal solution. 1: The 1st for loop will run till n-3 and 2nd for loop will run till n-2 instead of n, because we need to allocate last 2 elements to our two pointers and as per your code when i=n-2, j becomes n-1 (j=i+1=n-2+1) and num[k] throws out of bound exception, because k becomes n(k=j+1) 2: We could also increment low and decrement high when sumtarget respectively until there are duplicates for code to be more optimised. 3: Here's the more readable code(JAVA): class Solution { public List fourSum(int[] nums, int target) { int n = nums.length; List result = new ArrayList(); Arrays.sort(nums); for(int i=0; i
@_Arunvfx4 ай бұрын
hatsoff bro i was confused for 4 hours for optimal solution that passes all test cases.after seeing your comment it helped me to understand it.thankyou :)
@impalash_ag4 ай бұрын
@@_Arunvfx glad you finally understood it. Keep pushing forward 😃
@rockpaperscissors178124 күн бұрын
Afaik, it won't throw out of bound exception, because there is a check being done (k < l). In ur case, k takes n and l takes n - 1.
@shubhrajyotipoddar1684 Жыл бұрын
bro couldn't control his laugh during 3sum , 4sum intro and had to cut the clip 🤣
@omkarsawant926710 ай бұрын
Time Complexity: O(n log n) Space Complexity: O(n) #include #include #include using namespace std; vector fourSum(vector &nums, int target) { // sort the array sort(nums.begin(), nums.end()); // initialize result vector vector result; int n = nums.size(); // iterate through array with two pointers for (int i = 0; i < n; i++) { // skip duplicate elements if (i > 0 && nums[i] == nums[i - 1]) { continue; } for (int j = i + 1; j < n - 2; j++) { // skip duplicate elements if (j > i + 1 && nums[j] == nums[j - 1]) { continue; } // use two pointers for remaining elements int left = j + 1; int right = n - 1; while (left < right) { int currentSum = nums[i] + nums[j] + nums[left] + nums[right]; if (currentSum == target) { result.push_back({nums[i], nums[j], nums[left], nums[right]}); // skip duplicate elements while (left < right && nums[left] == nums[left + 1]) { left++; } while (left < right && nums[right] == nums[right - 1]) { right--; } // Move pointers left++; right--; } else if (currentSum < target) { // If the sum is less than the target, move the left pointer to the right left++; } else { // If the sum is greater than the target, move the right pointer to the left right--; } } } } return result; } int main() { // Example usage vector nums = {1, 0, -1, 0, -2, 2}; int target = 0; // Call the fourSum function vector result = fourSum(nums, target); // Print the unique quadruplets for (const auto &quadruplet : result) { cout
@akashchoudhary68764 ай бұрын
kuch bhi, same solution hai upar wala time complexity n3 hai, apki nlogn kaise aa gayi
@omkarsawant92674 ай бұрын
@@akashchoudhary6876 the overall time complexity of the fourSum function is O(nlogn+n^3)which simplifies to O(n^3)in Big O notation, assuming the input is sufficiently large.
@omkarsawant92674 ай бұрын
@@akashchoudhary6876 sorting in cpp will take O(nlogn)
@THEReFleX23 күн бұрын
@@akashchoudhary6876 hahahahah
@THUNDER-kw3wq4 ай бұрын
Striverr bhai ke liye toh 10some bhi asaan hai...Jokes apart hes the best teacher on youtube
@AbhimanyuKumar-pw3je Жыл бұрын
if u are stuck at test case 281 in leet code then :- class Solution { public: vector fourSum(vector& nums, int target) { int n =nums.size(); vector ans; sort(nums.begin(),nums.end()); for(auto it:nums) cout
@hareshnayak73028 ай бұрын
Understood,Thanks striver for this amazing video.
@shubha_jagadeeshАй бұрын
fell in love with your way of explanation❤
@asgarh4589 Жыл бұрын
People who are aware of 2 pointer approach, watch from 14:56, saves a lot of time.
@ankitadas5833 Жыл бұрын
Though I am little bit late, in this A to Z DSA Playlist But I covered All previous Videos within a week & now just completed 4 Sum. Striver Sir You are great! You will going to create a history in DSA course very soon around all over the World … I don't have the proper words to express my gratitude to you. All the Best Sir God bless you .
@ankitadas5833 Жыл бұрын
@@ArjunS-hi3vl Yes
@DR-mq1le Жыл бұрын
have you completed the course ? im also quite late in starting the course and moving at about the same pace as you
@ankitsharda1131 Жыл бұрын
@ankitadas5833 hey I would like to know if you have completed entire striver a2z dsa course? If yes, how much time did you take to complete entire course?
@bkmeher90056 ай бұрын
uffffffff.... understood , bahut bhala se bujhi hela bhai
@praveennemagoudar34427 ай бұрын
wow,....wow....wah..awesome expaination. undestood bro..hatsoff to you.
@gauravsoni542410 ай бұрын
best explanation on you tube
@MdKaif-bf3nz5 ай бұрын
Thank you so much Striver brother, you taught the 3sum and 4sum very clearly. 💀💀 ok jokes apart lol, this series is just too good, i can literally find every concept DSA related a lot of questions of each and every concept.
@Mythri33313 күн бұрын
Ahaaa... excellent explanation 😊
@kaichang81862 ай бұрын
understood, thanks for the perfect explanation
@helloworld2054 Жыл бұрын
This has to be the horniest coding problem 🌚
@jatinukey40623 ай бұрын
😂😂😂😂
@adebisisheriff159 Жыл бұрын
Striver..... you are amazing.................................... Thanks so much bro
@sarangkumarsingh79018 ай бұрын
Awesome Lecture Sir............
@ra-onegaming1383 Жыл бұрын
I have done this question using subsequence technique thanks u raj sir 😊 u teach really simple
@footballcreativeeverywhere260 Жыл бұрын
Great bhaiya ❤️🇮🇳 , ready for next one
@VishalGupta-xw2rp Жыл бұрын
After doing 4 Sum.... I think Sky is the limit 🌝
@kanyapandey Жыл бұрын
Thanks a ton for explaining in simplest way 🙏🙏🙏🙏🙏🙏
@akankshajain39974 ай бұрын
helpful to understand integer overflow
@gregorirasputin659 Жыл бұрын
Nice explanation. But the title is a bit too sus don't you think?
@isheep9025 Жыл бұрын
instead of taking elemens between j and k we can also take hashset elements to be between i and j it will also give coreect ans #include vector fourSum(vector& nums, int target) { int n = nums.size(); //size of the array set st; //checking all possible quadruplets: for (int i = 0; i < n; i++) { set hashset; for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { // taking bigger data type // to avoid integer overflow: long long sum = nums[i] + nums[j]; sum += nums[k]; long long fourth = target - sum; if (hashset.find(fourth) != hashset.end()) { vector temp = {nums[i], nums[j], nums[k], (int)(fourth)}; sort(temp.begin(), temp.end()); st.insert(temp); } // put the kth element into the hashset: } hashset.insert(nums[j]); } } vector ans(st.begin(), st.end()); return ans; }
@arunmahajan1028 Жыл бұрын
After Understanding this concept , I can Say that Sky is the limit..
@tejaspatel2212 Жыл бұрын
Imagine someone getting your video, on Searching 3sum video or 4sum video on google 🌚😂
@shis10 Жыл бұрын
Optimal Orgasm 🤣
@himanshukaushik9223 Жыл бұрын
😂😂
@D3vansh Жыл бұрын
I think first i should learn 3SUM then will do 4SUM as you said
@takeUforward Жыл бұрын
Please learn 2 sum, then 3 sum, then only 4 sum should be implemented ! Yes in terms of problem.
@gandhamphotos Жыл бұрын
@@takeUforward 😂😂
@shis10 Жыл бұрын
@@takeUforward 🤣🤣🤣
@godson200 Жыл бұрын
@@takeUforward sir but partner is not available how to do 2sum
@mohsin7343 Жыл бұрын
I am still with 1sum
@vagabondfoodie5788 Жыл бұрын
Aaj maza aa gya 3 sum karke🌚🌚🌚 ab 4 sum krunga
@Algorithmswithsubham Жыл бұрын
Sir app apne resume k project me konsa tech stack use kya haii. ❤ love your videos
@nquanta1548 Жыл бұрын
Thankyou for the wonderful series ❤️🔥
@shuvbhowmickbestin3 күн бұрын
Code is not running for this test case: nums = [-1, -1, -1] target = -2 if you're writing in a language like C# or Java where lists are considered reference types and the equality are based on references rather than actual values. The workaround in C# is to sort the list like usual and then use a *ValueTuple* to extract the values from the list and then adding this to the set as ValueTuples equality checks are value-based rather than reference which means two different valuetuples with the same numbers in the same order will still be considered as *EQUAL* whereas the same in a list would be considered as *UNEQUAL* due to a difference in references.
@ayanakash9900 Жыл бұрын
For optimal solution time complexity, we have to add sort() time too ig
@AniketKumar-hf2bo9 ай бұрын
understood,thnx for the amazing video ❤❤❤❤👌👌💕💕
@cinime Жыл бұрын
Understood! Super wonderful explanation as always, thank you so so much for your effort!!
@joeljacob4685 Жыл бұрын
Understood!! Thanks for the nice explanation
@AbhishekKumar-cv1dh Жыл бұрын
Understood beautifully!! 🔥❤
@rishabgupta2733 Жыл бұрын
The complexity of better solution is n^4 *logn according to codestudio
@PrinceTripathi-j7u10 ай бұрын
Nice to do 4 sum.
@satheeshbhukya9970 Жыл бұрын
excellent question....
@thomasshelby6780 Жыл бұрын
Tapped into pleasure back to back with 4Sum followed by 3Sum
@Lakshya-f4l4 ай бұрын
Mark for revision
@shivanichaudhary14278 ай бұрын
Amazing, thankyou!
@Manishgupta200 Жыл бұрын
It's amazing from brute to better to optimal similarly like 2Sum, 3Sum .. and my doubts in what's about 5-Sum, 6-Sum.. is any universal solution exist?
@harshshukla7675 Жыл бұрын
Yes, the complexity will keep on increasing tho
@myaccount090119754 ай бұрын
What about Time complexity for sorting the arrray? I know it will be lesser than N3 in this case. But still good to mention that!. Thanks for putting all the hard word!.
@himaniupadhyay8201 Жыл бұрын
Thanks Lord
@keshavgarg840710 күн бұрын
it is crazy how I am doing both 3sum and 4sum in a single day
@saurabhtamta72958 ай бұрын
why is it when i use:- long long sum = nums[i]+nums[j]+nums[k]+nums[l] -----it says runtime error but when i use :- long long sum = nums[i]; sum+=nums[j]; sum+=nums[k]; sum+=nums[l]; ----it works fine. Why is that?
@konankikeerthi5 ай бұрын
Thank you bro. Understood
@harshilpatel32053 ай бұрын
Understood 🙏🏻
@gautamsaxena46472 ай бұрын
understood bhaiya
@lwnflash21236 ай бұрын
for both brute and better ,you have missed the time complexity of inserting unique elements in the set.totally for better approach :O(n^3 *log(no of unique triplets* log m)...
@statuscreation94935 ай бұрын
while inserting elements in set we are inserting 3 to 4 elements onnly so it nearly O(1)
@ictfan-ly8gh13 күн бұрын
Bro u are wrong . If N is large there could be more elements. So it will be added@@statuscreation9493
@RituSingh-ne1mk11 ай бұрын
Understood!
@bikramprajapati5358 Жыл бұрын
Amazing Explanation
@hemantpatel14135 ай бұрын
Understood.
@Kj898653 ай бұрын
thanks for threesum
@UECAshutoshKumar6 ай бұрын
Thank you!
@raghavkansal3765 Жыл бұрын
you are the best 🧿🧿
@Shivi325904 ай бұрын
thank you
@senseiAree Жыл бұрын
Understood ❤
@hiteshpanchal5772 Жыл бұрын
Thxu striver bhai 😊😊
@mdazizul-y7b4 ай бұрын
understood sir
@RohitKumar-dy2gc7 ай бұрын
easy peasy✨✨
@the_avii_7Ай бұрын
Understood !!!!!
@himanshukaushik9223 Жыл бұрын
Bhaiya is everything fine any health issues video is not coming ??
@per.seus._ Жыл бұрын
understood ❤
@ArunsinghParihar-j3j9 ай бұрын
Understood🤙
@VishalPanwar-df5ek Жыл бұрын
Understood Srtiver!
@ravitejachandana61995 ай бұрын
understood !
@dewanandkumar858911 ай бұрын
Understood
@culeforever5408 Жыл бұрын
understood ;)
@DR-mq1le Жыл бұрын
understood , thank you!
@Ubaiish Жыл бұрын
i cant even watch dsa video without headphones now
@AbjSir Жыл бұрын
understood
@satyamsrivastava34237 ай бұрын
Just wanted to add the Time Complexity will also contain log(n) since we are sorting the array, right?
@203-aasritha29 ай бұрын
We should consider the time complexity for sorting the given array right ?
@lakshmiprasanna7058 Жыл бұрын
Understood 💯💯💯
@rishabpurba32682 ай бұрын
hey, does someone relate with this, i have trouble coming around the solution that i haven't come up with, i overthink and find it hard believing the solution?
@her_soulmate Жыл бұрын
Understood 🎉
@mranonymous19824 күн бұрын
Did 4sum by myself (brute, better, and optimal), after understanding 3sum. Ps: who's coming up with these names for the questions bruh..
@ShivamSingh-g2x6l9 ай бұрын
in the brute force approach when we first added nums[i]+nums[k]+nums[k]+nums[l] and then checked if its == target then it gives integer overflow error regardless of sum being stored in long long variable but if we do it one by one like sum = nums[i]+nums[j] and then sum+=nums[k] and sum+=nums[l] then the error is resolved can anyone please explain to me how its working.
@EC20022ELAKKIYAC9 ай бұрын
did you found how does it works ? same doubt here
@AbhishekBhattacharjee-j2m Жыл бұрын
UNDERSTOOD
@sanayaagarwal19079 ай бұрын
understood👍
@VivekKumar-kx2zf7 ай бұрын
why we are putting condition while(k
@brp35224 ай бұрын
If k crosses l then we have gone through all the possibilities. Imagine this if they crosses each other they will act like each other. After crossing K will give the elements L has already given and the same goes for L
@satyamjha-codeindwala6666 Жыл бұрын
satyam jha
@iamnoob75938 ай бұрын
US , THANK U
@inspiredomkar1239 Жыл бұрын
I didnt understand why are we calculating the sums separately for all the 4 variable : Why we need to do this :👇 ong sum = nums[i]; sum += nums[j]; sum += nums[left]; sum += nums[right]; when we can do simply this in one line 👇 long sum = nums[i] + nums[j] + nums[left] + nums[right];
@udayprakashharsh2805 Жыл бұрын
This is because the nums[i] is int so if we assume all the 4 quad to be 1e9 then the result would be 4*1e9 which will lead to overflow int error to avoid this we are separately calculating the sum for all the 4 array elements.
@SagarKumar-li4um3 ай бұрын
@@udayprakashharsh2805but sum is already declared as long long then there should not be any problem of overflow either we do the sum in one step or in 4 steps