4 Sum | Brute - Better - Optimal with Codes

Sdílet
Vložit
  • čas přidán 9. 07. 2024
  • Problem Link: bit.ly/3It5SyP
    Notes/C++/Java/Python codes: takeuforward.org/data-structu...
    We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
    Full Course: bit.ly/tufA2ZYt
    You can follow me across social media, all my handles are below:
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    0:00 Introduction of Course
    00:54 Problem Statement
    02:24 Brute force approach
    03:08 Code
    05:38 Complexity
    06:14 Better approach
    11:31 Code
    13:22 Complexity
    14:54 Optimal approach
    21:59 Code
    27:04 Complexity

Komentáře • 255

  • @takeUforward
    @takeUforward  Před 10 měsíci +12

    Please watch our new video on the same topic: czcams.com/video/eD95WRfh81c/video.html

    • @uniqueyuvrajgaming3392
      @uniqueyuvrajgaming3392 Před 5 měsíci

      You said it is O(n3) but sorting will take some time to what about that?

    • @vaibhavagarwal1479
      @vaibhavagarwal1479 Před 5 měsíci

      ​@@uniqueyuvrajgaming3392nlog n is basically negligible in comparision of n3 thats why is it not included

    • @user-hb2ex3wg9k
      @user-hb2ex3wg9k Před 29 dny

      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.

    • @user-hb2ex3wg9k
      @user-hb2ex3wg9k Před 29 dny +1

      @@uniqueyuvrajgaming3392 Sorting will have always 4 elements which will take constant time.

  • @shailesh_rajpurohit
    @shailesh_rajpurohit Před 11 měsíci +143

    Note: 0:42 Make sure you watch the 3Sum before doing the 4Sum .
    🙂

  • @yashsharma6396
    @yashsharma6396 Před rokem +166

    Excited to do 4sum after completing 3sum yesterday

    • @gareer2436
      @gareer2436 Před rokem +36

      Damn bro include me next time

    • @yashsharma6396
      @yashsharma6396 Před rokem +31

      ​@@gareer2436 you know how to bend?😂😂

    • @gregorirasputin659
      @gregorirasputin659 Před rokem +20

      Yaar tum log bohat tharki ho 😂

    • @gareer2436
      @gareer2436 Před rokem

      @@yashsharma6396 no but I know what to do when someone bends...😏
      Lay them down and..
      Massage their backs of course!

    • @gareer2436
      @gareer2436 Před rokem +13

      @@gregorirasputin659 ma to sath me problem solve krne ki baat kr raha tha bro... tum kya soch liye?😏

  • @jatinsareen7771
    @jatinsareen7771 Před rokem +63

    3 sum was awesome, 4 sum was fantastic!!!!

  • @Akash-yr2if
    @Akash-yr2if Před 11 měsíci +30

    0:44 Striver's Smile Says it all

    • @brij2695
      @brij2695 Před 11 měsíci +1

      you got my point bro😁

    • @AkOp-bf9vm
      @AkOp-bf9vm Před měsícem

      bhai bhai🫡🫡😂. what a detailing bro

  • @takeUforward
    @takeUforward  Před rokem +61

    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
      @rishav144 Před rokem +4

      striver ..king of DSA

    • @shra1
      @shra1 Před rokem

      The if condition inside while to check if(k

    • @codemania2878
      @codemania2878 Před 11 měsíci

      sir which whiteboard pen app you use for explain the code

    • @itsmrcrazy7670
      @itsmrcrazy7670 Před 10 měsíci

      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

    • @user-lz1rv3em5u
      @user-lz1rv3em5u Před 10 měsíci

      @@codemania2878 He is using an iPad and its default Notes app​

  • @ashishashish1403
    @ashishashish1403 Před 22 dny +3

    Whole video is about 4 sum but 0:40 to 0:50 is wholesome

  • @arujgarg7267
    @arujgarg7267 Před rokem +37

    Finally did 4 sum, amazing experience

    • @gareer2436
      @gareer2436 Před rokem +9

      Can you elaborate your experience? Asking for a friend

    • @catalyst8654
      @catalyst8654 Před rokem +1

      can you explain about your experience...🙂🙂

    • @leisuretime4198
      @leisuretime4198 Před rokem

      @@catalyst8654 🤣🤣

    • @Akash-yr2if
      @Akash-yr2if Před rokem +1

      Would you take a moment to share your experience with us.

  • @avinashkumar2411
    @avinashkumar2411 Před rokem +7

    You should not take I upto

  • @selvaarumugam370
    @selvaarumugam370 Před rokem +8

    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.

  • @mehulthuletiya497
    @mehulthuletiya497 Před rokem +30

    00:54 Problem Statement
    02:24 Brute force approach
    03:08 Code
    05:38 Complexity
    06:14 Better approach
    11:31 Code
    13:22 Complexity
    14:54 Optimal approach
    21:59 Code
    27:04 Complexity

    • @takeUforward
      @takeUforward  Před rokem +39

      You watch all the videos? I see you daily updating me with timestamps, massive thanks for that.

    • @mehulthuletiya497
      @mehulthuletiya497 Před rokem +22

      ​​@@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
      @salihedneer8975 Před 9 měsíci +1

      @@takeUforward striver please update timestamp in disucssion so that ppl can access it easily

  • @AbhishekKumar-cv1dh
    @AbhishekKumar-cv1dh Před rokem

    Understood beautifully!! 🔥❤

  • @hareshnayak7302
    @hareshnayak7302 Před 3 měsíci +1

    Understood,Thanks striver for this amazing video.

  • @kanyapandey
    @kanyapandey Před rokem

    Thanks a ton for explaining in simplest way 🙏🙏🙏🙏🙏🙏

  • @nquanta1548
    @nquanta1548 Před rokem

    Thankyou for the wonderful series ❤️🔥

  • @impalash_ag
    @impalash_ag Před 8 dny +1

    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

    • @_Arunvfx
      @_Arunvfx Před 7 dny +1

      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_ag
      @impalash_ag Před 7 dny

      @@_Arunvfx glad you finally understood it. Keep pushing forward 😃

  • @joeljacob4685
    @joeljacob4685 Před 10 měsíci

    Understood!! Thanks for the nice explanation

  • @praveennemagoudar3442
    @praveennemagoudar3442 Před 2 měsíci +1

    wow,....wow....wah..awesome expaination. undestood bro..hatsoff to you.

  • @ra-onegaming1383
    @ra-onegaming1383 Před rokem

    I have done this question using subsequence technique thanks u raj sir 😊 u teach really simple

  • @bkmeher9005
    @bkmeher9005 Před měsícem

    uffffffff.... understood , bahut bhala se bujhi hela bhai

  • @dishankbaid05
    @dishankbaid05 Před rokem +33

    The consistency level of striver is really the source of motivation 🔥.

  • @shivanichaudhary1427
    @shivanichaudhary1427 Před 3 měsíci

    Amazing, thankyou!

  • @bikramprajapati5358
    @bikramprajapati5358 Před rokem

    Amazing Explanation

  • @footballcreativeeverywhere260

    Great bhaiya ❤️🇮🇳 , ready for next one

  • @AniketKumar-hf2bo
    @AniketKumar-hf2bo Před 5 měsíci

    understood,thnx for the amazing video ❤❤❤❤👌👌💕💕

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 Před 3 měsíci

    Awesome Lecture Sir............

  • @gauravsoni5424
    @gauravsoni5424 Před 5 měsíci

    best explanation on you tube

  • @DR-mq1le
    @DR-mq1le Před 11 měsíci

    understood , thank you!

  • @adebisisheriff159
    @adebisisheriff159 Před 7 měsíci

    Striver..... you are amazing.................................... Thanks so much bro

  • @satheeshbhukya9970
    @satheeshbhukya9970 Před 9 měsíci

    excellent question....

  • @cinime
    @cinime Před rokem +1

    Understood! Super wonderful explanation as always, thank you so so much for your effort!!

  • @konankikeerthi
    @konankikeerthi Před 29 dny

    Thank you bro. Understood

  • @AbhimanyuKumar-pw3je
    @AbhimanyuKumar-pw3je Před 9 měsíci +3

    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

  • @UECAshutoshKumar
    @UECAshutoshKumar Před měsícem

    Thank you!

  • @raghavkansal3765
    @raghavkansal3765 Před rokem

    you are the best 🧿🧿

  • @senseiAree
    @senseiAree Před 9 měsíci

    Understood ❤

  • @lakshmiprasanna7058
    @lakshmiprasanna7058 Před rokem

    Understood 💯💯💯

  • @rajatpatra2593
    @rajatpatra2593 Před rokem

    Understood ❤️

  • @MdKaif-bf3nz
    @MdKaif-bf3nz Před 28 dny +1

    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.

  • @ravitejachandana6199
    @ravitejachandana6199 Před 20 dny

    understood !

  • @user-sm7zo5zd9t
    @user-sm7zo5zd9t Před 4 měsíci

    Understood🤙

  • @user-nb1ye5tf1r
    @user-nb1ye5tf1r Před 6 měsíci

    Nice to do 4 sum.

  • @Shivi32590
    @Shivi32590 Před dnem

    thank you

  • @simonchristopherp3260
    @simonchristopherp3260 Před 4 měsíci

    Understood!

  • @dhairyashiil
    @dhairyashiil Před rokem

    Thank You : )

  • @per.seus._
    @per.seus._ Před 11 měsíci +1

    understood ❤

  • @helloworld2054
    @helloworld2054 Před 10 měsíci +6

    This has to be the horniest coding problem 🌚

  • @hiteshpanchal5772
    @hiteshpanchal5772 Před 11 měsíci

    Thxu striver bhai 😊😊

  • @Str13011
    @Str13011 Před rokem

    Keep going 👍

  • @her_soulmate
    @her_soulmate Před 10 měsíci

    Understood 🎉

  • @himaniupadhyay8201
    @himaniupadhyay8201 Před 11 měsíci +1

    Thanks Lord

  • @VishalPanwar-df5ek
    @VishalPanwar-df5ek Před 11 měsíci

    Understood Srtiver!

  • @subhamcoder
    @subhamcoder Před rokem +3

    Sir app apne resume k project me konsa tech stack use kya haii. ❤ love your videos

  • @sanayaagarwal1907
    @sanayaagarwal1907 Před 4 měsíci

    understood👍

  • @asgarh4589
    @asgarh4589 Před rokem

    People who are aware of 2 pointer approach, watch from 14:56, saves a lot of time.

  • @hemantpatel1413
    @hemantpatel1413 Před 24 dny

    Understood.

  • @user-nk1mb5fy7j
    @user-nk1mb5fy7j Před rokem

    UNDERSTOOD

  • @RohitKumar-dy2gc
    @RohitKumar-dy2gc Před 3 měsíci

    easy peasy✨✨

  • @203-aasritha2
    @203-aasritha2 Před 5 měsíci

    We should consider the time complexity for sorting the given array right ?

  • @ayanakash9900
    @ayanakash9900 Před 10 měsíci +2

    For optimal solution time complexity, we have to add sort() time too ig

  • @soumiyamuthuraj3516
    @soumiyamuthuraj3516 Před 25 dny

    Awesome

  • @gregorirasputin659
    @gregorirasputin659 Před rokem +6

    Nice explanation. But the title is a bit too sus don't you think?

  • @tanishagarwal5992
    @tanishagarwal5992 Před 6 měsíci

    When striver submits on Code Studio , it also showed him time complexity as O(n4 log n) but it doesn't show in mine. Why?

  • @VishalGupta-xw2rp
    @VishalGupta-xw2rp Před 9 měsíci +4

    After doing 4 Sum.... I think Sky is the limit 🌝

  • @ankitadas5833
    @ankitadas5833 Před rokem +2

    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
      @ankitadas5833 Před rokem

      @@ArjunS-hi3vl Yes

    • @DR-mq1le
      @DR-mq1le Před 11 měsíci

      have you completed the course ? im also quite late in starting the course and moving at about the same pace as you

    • @ankitsharda1131
      @ankitsharda1131 Před 8 měsíci

      @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?

  • @culeforever5408
    @culeforever5408 Před 8 měsíci +1

    understood ;)

  • @AbjSir
    @AbjSir Před 8 měsíci

    understood

  • @user-st9ff2vh6l
    @user-st9ff2vh6l Před 6 měsíci

    Understood

  • @rishabgupta2733
    @rishabgupta2733 Před 9 měsíci

    The complexity of better solution is n^4 *logn according to codestudio

  • @SiddheshKukade
    @SiddheshKukade Před 11 měsíci

    thanks

  • @shubhrajyotipoddar1684
    @shubhrajyotipoddar1684 Před rokem +1

    bro couldn't control his laugh during 3sum , 4sum intro and had to cut the clip 🤣

  • @Manishgupta200
    @Manishgupta200 Před rokem +7

    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?

  • @omkarsawant9267
    @omkarsawant9267 Před 6 měsíci +1

    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

    • @akashchoudhary6876
      @akashchoudhary6876 Před 3 dny

      kuch bhi, same solution hai upar wala time complexity n3 hai, apki nlogn kaise aa gayi

    • @omkarsawant9267
      @omkarsawant9267 Před 3 dny

      @@akashchoudhary6876 the overall time complexity of the fourSum function is O(nlog⁡n+n^3)which simplifies to O(n^3)in Big O notation, assuming the input is sufficiently large.

    • @omkarsawant9267
      @omkarsawant9267 Před 3 dny

      @@akashchoudhary6876 sorting in cpp will take O(nlogn)

  • @satyamsrivastava3423
    @satyamsrivastava3423 Před 2 měsíci

    Just wanted to add the Time Complexity will also contain log(n) since we are sorting the array, right?

  • @iamnoob7593
    @iamnoob7593 Před 3 měsíci

    US , THANK U

  • @vagabondfoodie5788
    @vagabondfoodie5788 Před rokem +2

    Aaj maza aa gya 3 sum karke🌚🌚🌚 ab 4 sum krunga

  • @himanshukaushik9223
    @himanshukaushik9223 Před rokem +1

    Bhaiya is everything fine any health issues video is not coming ??

  • @syedFAHIM-el1wr
    @syedFAHIM-el1wr Před rokem

    Under stood

  • @thomasshelby6780
    @thomasshelby6780 Před rokem +2

    Tapped into pleasure back to back with 4Sum followed by 3Sum

  • @srikarvinjamara2003
    @srikarvinjamara2003 Před 7 měsíci

    Hi, thanks for the video explanation. I had one doubt. When we get the sum==target, we move the k and l pointers front and back respectively [not just once but until we get a different value for k and l pointers so as to not repeat the same ans]. Why are we not doing the same when {sumtarget}. [i.e. in these 2 cases we just do k+=1 or l-=1; shouldn't we here also move the k and l pointers ahead until we get a different value for them.] Please let me know if possible. Thanks!

    • @203-aasritha2
      @203-aasritha2 Před 5 měsíci +1

      It goes the same as well. If we move the pointer and again we get the same set , the sum will be the same right ? therefor if the sum is less than zero , it remains the same and vice versa. Then the iteration completes and we will hit the same condition. This goes until we get another pair.

  • @D3vansh
    @D3vansh Před rokem +11

    I think first i should learn 3SUM then will do 4SUM as you said

    • @takeUforward
      @takeUforward  Před rokem +36

      Please learn 2 sum, then 3 sum, then only 4 sum should be implemented ! Yes in terms of problem.

    • @gandhamphotos
      @gandhamphotos Před rokem +8

      @@takeUforward 😂😂

    • @shis10
      @shis10 Před rokem +3

      @@takeUforward 🤣🤣🤣

    • @godson200
      @godson200 Před rokem +8

      @@takeUforward sir but partner is not available how to do 2sum

    • @mohsin7343
      @mohsin7343 Před rokem +10

      I am still with 1sum

  • @AkshayYadaveeb
    @AkshayYadaveeb Před 3 měsíci

    while i'm taking sum==target case in else it is not working like we did in three sum and if i put if(sum==target) at initial position it is working . why it is so ?

  • @many987
    @many987 Před rokem

    understood.

  • @myaccount09011975
    @myaccount09011975 Před 2 dny

    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!.

  • @tejaspatel2212
    @tejaspatel2212 Před rokem +7

    Imagine someone getting your video, on Searching 3sum video or 4sum video on google 🌚😂

  • @arunmahajan1028
    @arunmahajan1028 Před 8 měsíci

    After Understanding this concept ,
    I can Say that Sky is the limit..

  • @saurabhtamta7295
    @saurabhtamta7295 Před 3 měsíci +1

    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?

  • @hydrocy.9165
    @hydrocy.9165 Před 3 měsíci

    23:16 shouldnt j= i+1 will be second element? I cant understand

  • @user-mk3jx1bs2g
    @user-mk3jx1bs2g Před 5 měsíci +1

    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.

    • @EC20022ELAKKIYAC
      @EC20022ELAKKIYAC Před 4 měsíci

      did you found how does it works ? same doubt here

  • @jayanthvanamamala1425

    Bhaiya , Can uh please post an article about k sum similiar to this type , which is applicable for all k sums . It can help to build intution!!!!

  • @chathuryachenchupalli6535

    Hi sir
    We request you
    Can you please make a video on Alex goes Shopping problem. We cannot understand the question.

  • @aysams2
    @aysams2 Před rokem

    04:20 - what does exceeding the integer limit mean? (when we are storing it in a long long)

    • @takeUforward
      @takeUforward  Před rokem +1

      if you store in int it might exceed

    • @SANDEEPKUMAR-in6li
      @SANDEEPKUMAR-in6li Před rokem

      @@takeUforward but in 3 sum we sum up like sum = nums[i]+nums[j]+nums[k] but in this 4 sum you sum up all the indices separately which is correct but I am getting error in a test case when I am doing sum = nums[i]+ nums[j]+ nums[k]+ nums[l]; why?

    • @Monika-vs6bw
      @Monika-vs6bw Před 10 měsíci

      @@SANDEEPKUMAR-in6li the compiler has an algorithm for adding these numbers . while adding four numbers(big numbers) the algorithm needs a lot of space , but while adding two or three the space is under limit. hence adding them one by one is recomended.

  • @VivekKumar-kx2zf
    @VivekKumar-kx2zf Před 2 měsíci

    why we are putting condition while(k

    • @brp3522
      @brp3522 Před 5 dny

      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

  • @anupamkhare7041
    @anupamkhare7041 Před rokem

    is this the end..or more videos will come....I'm about to start this course.

  • @anujraut426
    @anujraut426 Před rokem

    Gr8

  • @daminkhan5105
    @daminkhan5105 Před měsícem

    In the better approach why the code didn't gave runtime error for j and k when i = n - 1, because j = i + 1 and k = j + 1, making j and k going out of bounds of array so it should've thrown the error right?

    • @statuscreation9493
      @statuscreation9493 Před 12 dny

      while starting for loop we also stated j and k should be less than n if it goes out of bound whole loop will end.. so no chance

    • @daminkhan5105
      @daminkhan5105 Před 12 dny

      @@statuscreation9493 ohh got it thankyou bro

  • @SunnyKumar-dw9ze
    @SunnyKumar-dw9ze Před rokem

    Tree ka v new playlist aayega kya...

  • @himanshukaushik9223
    @himanshukaushik9223 Před rokem +1

    Bhaiya s.c in optimal approach return ans ki hai O(no of quad) ??

    • @takeUforward
      @takeUforward  Před rokem +1

      jitne quads hongey answer me utna hi space lagega bro

    • @himanshukaushik9223
      @himanshukaushik9223 Před rokem +2

      @@takeUforward space answer return ma use hora ha na solve ma nahi hora na

  • @gugulothsrilakshmi8729
    @gugulothsrilakshmi8729 Před 8 měsíci

    Why removing duplicates I'm not getting the concept in 3 sum problem too in optimal solution

  • @SriramRajaniyer
    @SriramRajaniyer Před 7 měsíci

    In the optimal code solution, why are we checking for k against k + 1, It seems to work for :
    arr[k] == arr[k - 1]

    • @dhrumilparmar1505
      @dhrumilparmar1505 Před 5 měsíci

      this will work but at end. k will have value of last element of (common element sub array) e.g. [1,1,1,0,0,0,0,0,0]// after loop k will be 3 .
      its cleaver trick to break loop and when access k. it will give new value (different from copy elements).
      if we first decrese k(usual procedure of algo. nothing extra done); and then check arr[k] == arr[k + 1] in loop,
      k- - // k=7
      e.g. [1,1,1,0,0,0,0,0,0]
      // after loop breaks, k will be 2