2 Sum Problem | 2 types of the same problem for Interviews | Brute-Better-Optimal

Поделиться
HTML-код
  • Опубликовано: 22 авг 2024
  • Notes/C++/Java/Python codes: takeuforward.o...
    Problem links.
    2 Sum: bit.ly/3Iu7zMu
    We have solved the problem, and we have gone from brute force and ended with the most optimal solution.
    Full Course: bit.ly/tufA2ZYt
    You can follow me across social media, all my handles are below:
    Linkedin/Instagram/Telegram: linktr.ee/take...
    0:00 Introduction of course

Комментарии • 423

  • @takeUforward
    @takeUforward  Год назад +88

    Let's march ahead, and create an unmatchable DSA course! ❤
    Timestamps pleaseeee
    Use the problem links in the description.

    • @prithammahato1915
      @prithammahato1915 Год назад +1

      0.45 - BRUTE SOL
      5.13 - BETTER APPROACH (USING HASHING)
      11.55 - OPTIMAL APPROACH (USING 2 POINTER)

    • @prithammahato1915
      @prithammahato1915 Год назад

      0.45
      BRUTE
      5.13
      BETTER
      11.55
      OPTIMAL

    • @amitranjan6998
      @amitranjan6998 Год назад

      @take U forward : Hi Striver Bhaiya , I love your video , I am learning alot from you , just want to highlight in SDE sheet for this problem it's redirecting to some other question (i.e Find all pairs with a given sum) ,but it's similiar to 2sum approach only :)

    • @RamanKumar-er8ie
      @RamanKumar-er8ie Год назад

      it does not work on tc=-3,-2,2,3,3 any please help me out in this

    • @abhinavennala9613
      @abhinavennala9613 Год назад

      @@RamanKumar-er8ie what is the target?

  • @habeeblaimusa4466
    @habeeblaimusa4466 Год назад +208

    Bro, I am a guy from Africa you are the first person in this tech community that inspires me that I can do it..
    Hats off for you bro 🔥🔥🔥

    • @kushalchakraborty6970
      @kushalchakraborty6970 Год назад

      Bro ,I need some tips to increase my 🍆?

    • @kulkarnisoham
      @kulkarnisoham Год назад +24

      That's a complement to whole country..!!

    • @Fe-ironman
      @Fe-ironman 7 месяцев назад

      no@@kulkarnisoham

    • @barnam_das
      @barnam_das 5 месяцев назад +4

      yes my boy, you can do it lets gooooo !!!!

    • @ArvindSingh-wj7vy
      @ArvindSingh-wj7vy 3 месяца назад +3

      Thank you from India on behalf of Striver 😂

  • @pragmaticcoder6910
    @pragmaticcoder6910 9 месяцев назад +20

    I can’t thank enough because you teach so well about the concepts of DSA compared to others. None of the paid resources out there have good content. All I can say is May God bless you with long life and happiness. You are making all of us to land in our dream job.

  • @engineerbychance7449
    @engineerbychance7449 4 месяца назад +111

    i got rejected from an interviwew on 29th , because of dsa it was for 18lpa and remote , but i promise the very next interview i have for dsa i will crack it and i will learn all concepts only from this channel

    • @RanveerSingh-2864
      @RanveerSingh-2864 4 месяца назад +8

      Keep it up comeback and comment after you crack it🤜🤛

    • @ravikr3463
      @ravikr3463 4 месяца назад +5

      Hua kya

    • @engineerbychance7449
      @engineerbychance7449 4 месяца назад

      @@ravikr3463 bhai trees puch lie 🥲 mai 4 saal se Sirf dev kar raha hu frontend react back end node and express db MySQL

    • @shadowslayer2248
      @shadowslayer2248 3 месяца назад +7

      Same thing bro. Scared to sit for placements because of DSA. But I'm sure after finishing Striver's Sheet, every problem will be a piece of cake.

    • @user-hp9kj8qt1h
      @user-hp9kj8qt1h 2 месяца назад +2

      I've just got in 2nd yr and done nothing till now. Any tip for me 🙏

  • @09ankurjaiswal85
    @09ankurjaiswal85 10 месяцев назад +37

    00:06 The video covers the 'two sum problem' and its two varieties.
    02:04 Two sum problem can be solved using brute force method
    04:05 Optimizing 2 Sum problem using a better solution
    06:08 Using hashing to easily retrieve elements from a data structure.
    08:13 Find the indexes of two elements in an array
    10:30 Two pointer approach is a slightly better solution to solve the problem without using a map data structure.
    12:38 Using the two-sum problem to find pairs that add up to a given target
    14:51 The time complexity of the algorithm is O(n)
    16:49 Space complexity and array manipulation explanation

  • @suravighosal9934
    @suravighosal9934 Год назад +15

    We need more problem solving videos. Explanation is super. I have gone through a few channels but this is the best! Thank you so much.❤️

  • @harshavardhan184
    @harshavardhan184 Год назад +40

    This is the consistency we need from you bhai! 🔥

  • @aryansinha1818
    @aryansinha1818 Год назад +41

    Man you have changed a lot, this new version feels like a super saiyan mode.

  • @many987
    @many987 Год назад +9

    bhaiya you're so much doing for us, even you live alone in Poland, you sacrifice your time for you and I understand every thing you teach as you are so good in explaining and making people understand. once again thanks a lot for this

  • @parthkolgiri7501
    @parthkolgiri7501 Год назад +10

    When he introduced two pointer method my mind was like 🤯💥 Thank you striver!!!

  • @itzmartin20
    @itzmartin20 11 месяцев назад +4

    Understood! Thanks a billion for your top-notch explaination brother!

  • @Josuke217
    @Josuke217 Месяц назад +1

    Man after watching the first 5 videos and solving the problems on my own , I was also able to solve almost all medium and some hard problems. Watching these videos really built my logic , these lectures are a gold mine.

    • @Josuke217
      @Josuke217 Месяц назад

      I didn't think about hashing but you mentioned it, I quickly coded it and got the answer. Now I just have to think of these approaches quickly

    • @Squirt_Turtle
      @Squirt_Turtle Месяц назад +2

      Yes even i can solve any problem using brute force approach for better & optimal i have to think....i m sure till the course end i will able to improve my solving skills..i belive ❤

  • @PrithaMajumder
    @PrithaMajumder 2 месяца назад +4

    Raj, Thanks a lot for This Amazing Video about C++ Arrays
    Video - 5 Completed ✅

  • @secretcoder7
    @secretcoder7 Год назад +3

    GREAT BHAIYA I buy multiple dsa courses but i only understand with ur lectures
    🤗🤗

  • @parakhpatel93
    @parakhpatel93 8 месяцев назад +2

    this 5-star add is just crazy!!

  • @md.ualiurrahmanrahat2400
    @md.ualiurrahmanrahat2400 Год назад +3

    Done this video. Amazing explanation. Learning amazing. Growing amazingly.

  • @tanmaykarn
    @tanmaykarn Год назад +2

    goddamn, maybe it was the first time, I understood the approach from the video and coded it on my own and got it accepted. God complex bruhh

  • @AbjSir
    @AbjSir 10 месяцев назад +2

    2 pointer approach was very beautiful

  • @harshitrautela6585
    @harshitrautela6585 7 месяцев назад +1

    🎯 Key Takeaways for quick navigation:
    00:45 📚 *The video is part of the Striver's A to Z DSA course, covering comprehensive content with a guarantee of deep understanding in DS algo for interviews worldwide.*
    01:12 🎯 *The Two Sum problem has two varieties: one asks for a yes or no if a pair with the given sum exists, and the other requires returning the indices of the pair.*
    02:50 🧠 *The initial Brute Force solution involves nested loops to check every pair's sum.*
    05:24 ⏩ *The improved solution uses hashing, mapping array elements to their indices, reducing time complexity to O(n).*
    09:17 🚀 *The optimal approach, sorting the array and using two pointers, achieves O(n log n) time complexity without extra space.*
    15:16 🔄 *The space complexity for the two-pointer approach is O(1), considering no additional space is used.*
    17:50 📂 *Code for all three approaches (Brute Force, Hashing, Two-Pointer) is available in C++, Java, and Python in the video description.*
    Made with HARPA AI

  • @KomalSingh-qr4mu
    @KomalSingh-qr4mu 15 дней назад

    Understood ! This is the first ques of my challenge 😃

  • @sagnikbose5823
    @sagnikbose5823 Год назад +6

    Bhaiya thanks for understanding from the student perspective. You are doing a really great job. Looking forward to the other problem solving videos from you. Good luck!

  • @sunilkamat8199
    @sunilkamat8199 Год назад +2

    Brute -> Better > Optimal = BEST👌

  • @hareshnayak7302
    @hareshnayak7302 5 месяцев назад +1

    Understood, Thank you strivers for this amazing video.

  • @anamikarawat3698
    @anamikarawat3698 Год назад +9

    Thankss bhaiya for this consistency ❤️🙌
    My placement is coming soon I'm in 6th semester!!🙂

    • @legendrose6828
      @legendrose6828 Год назад

      me too, can we connect on linkedin?

    • @jayrathod7957
      @jayrathod7957 9 месяцев назад

      Hello bhaiya how was it?
      I'm here at 3rd sem 🤔

    • @chiragdusad2015
      @chiragdusad2015 5 месяцев назад

      Hey!
      Do you get the placements or how was your experience?

  • @user-pi1yf9wg4p
    @user-pi1yf9wg4p 29 дней назад

    I am actually preparing for my placements,now im btech 3 rd yr and i wanted someone who says from the scratch with the bestttttttt explanation.........thankyou soo muchh bro...i will follow each and every video of urs and i hope i can crack it....!!!

  • @sagestudy
    @sagestudy Год назад +3

    You are Great Sir! I love your explanation !!

  • @harshukey6072
    @harshukey6072 Год назад +2

    Understood 🎉
    40 lakh

  • @sreescorner
    @sreescorner 11 месяцев назад +2

    Thanks a lot, very clear explanation

  • @movocode
    @movocode Месяц назад +1

    10:36
    mpp.insert({nums[i], i}); is more optimised than
    mpp[num] = i; (used in video)

  • @jay8118
    @jay8118 Год назад +2

    Job ke saath saath Padhana koi inse sikhe..🙌🙌

  • @sayakghosh5104
    @sayakghosh5104 Год назад +1

    Understood!!!...Great as always. :)

  • @cinime
    @cinime Год назад

    Understood! Super awesome explanation as always, thank you very very much for your effort!!

  • @yashwanthostwal6947
    @yashwanthostwal6947 Год назад

    int main() {int n=3;
    int array[3]={3,2,4}; int target=6;
    for(int i=0; i

  • @arunimachakraborty1175
    @arunimachakraborty1175 9 месяцев назад +1

    Awesome explanation, thanks a lot

  • @kapilraivines8486
    @kapilraivines8486 4 месяца назад

    Hey Striver I am from Jupiter... I love your DSA playlist

  • @nagame859
    @nagame859 Год назад +1

    Top notch 🤩. Understood

  • @bhavyasharma3500
    @bhavyasharma3500 Год назад +2

    Hats off to you striver bro

  • @tamilmukbang3789
    @tamilmukbang3789 4 месяца назад +1

    understood. Thank youuuu

  • @Nishantkumar-oh9th
    @Nishantkumar-oh9th Год назад +1

    HE just killed it as it says🤗

  • @shubhamlipane8634
    @shubhamlipane8634 Год назад +1

    Understood
    Thank You :-)

  • @poojadasari8140
    @poojadasari8140 Год назад

    Understood. Very clear explanation

  • @SigmaEditsbr
    @SigmaEditsbr 5 месяцев назад

    Thanks sir for provide us this type of content ❤❤❤

  • @m_fi8926
    @m_fi8926 13 дней назад

    in the brute force approach you should have initialized j=i+1;

  • @DeepakKumar-xj4ul
    @DeepakKumar-xj4ul 5 месяцев назад

    understood everything Thanks a lot sir!

  • @banothutharun2743
    @banothutharun2743 5 месяцев назад +1

    understood sir

  • @saddatsabanature4307
    @saddatsabanature4307 4 месяца назад

    solve in python
    $$$
    x=list(map(int,input().split()))
    x.sort()
    y=int(input())
    for i in range(y):
    a=(y-x[i])
    if (a in x):
    p=x.index(a)
    print(i,p)
    break
    else:
    continue
    time complexity 0(n)

  • @m.nirupreddy8001
    @m.nirupreddy8001 Год назад

    Understood! great explanation

  • @kingbadshah452
    @kingbadshah452 7 месяцев назад

    understood everything .... thanks striver

  • @nihalshaikh196
    @nihalshaikh196 5 месяцев назад +3

    Using unordered_map we can do it in O(n) and even if we use ordered map we can do it in O(n*logn) and for two pointer approach we are sorting first so it is taking O(n*logn) so how is that optimal approach?

    • @arpit3242
      @arpit3242 2 месяца назад +2

      Two pointer approach is not that much optimal approach, but it is used when we want to find the answer without "map" data structure.

  • @user-sg1jx3ic4q
    @user-sg1jx3ic4q 4 месяца назад

    understood brother💞

  • @AnjuGupta-sz1uk
    @AnjuGupta-sz1uk 11 месяцев назад

    understood bhaiya! really well explained..

  • @shafiulhaque6583
    @shafiulhaque6583 Год назад +3

    Thanks to you for the video Sir .
    Had a query : In Type 2 of the problem , why do we need the extra Data structure for the indices ? Because I think "left" and "right" these should be giving us the indices of the 2 numbers whose sum equals to the target .

    • @aashishomre1624
      @aashishomre1624 Год назад +5

      Aftet sorting indices are changed

    • @himanshukaushik9223
      @himanshukaushik9223 Год назад

      ​@@aashishomre1624 bhai return hum curly braces ma kyu nahi kar sakta hai

    • @aashishomre1624
      @aashishomre1624 Год назад

      Bcoz return type function ka vector hai {-1,-1} indicating no pair found

    • @aashishomre1624
      @aashishomre1624 Год назад

      vector vect{ 10, 20, 30 }; this syntax is used

  • @charuwaka1
    @charuwaka1 5 месяцев назад

    3:33 here you can keep j=i+1 instead of 0 so you don' need to write i==j

  • @ArpanChakraborty-do6yz
    @ArpanChakraborty-do6yz 7 месяцев назад +1

    Understood ❤

  • @lifehustlers164
    @lifehustlers164 10 месяцев назад +1

    Completed 5/28!!!

  • @samuelfrank1369
    @samuelfrank1369 10 месяцев назад

    Understood. Thanks a lot. Make More videos please......

  • @mohithvaishnav4317
    @mohithvaishnav4317 Год назад

    you jus make things easier..

  • @user-yl3wu4th5i
    @user-yl3wu4th5i 6 месяцев назад

    The question is to find the indices of the elements which on adding give us the target value. In the last part of video, where we need to find the solution in O(1) space, we sort the array and hence lose the indices of input array. So how can we return the indices of elements then ?

  • @Mani_0962
    @Mani_0962 5 месяцев назад

    1.Understood

  • @user-zn3be9ik1x
    @user-zn3be9ik1x Год назад +2

    Pls make videos on sliding windows types questions and the types

  • @haramritsingh6521
    @haramritsingh6521 Год назад +1

    Understood !

  • @ameyshinde4297
    @ameyshinde4297 Год назад +6

    // Two Sum in Java using HashMap TC --> O(N)
    import java.util.*;
    class HelloWorld {
    static int[] twoSum(int arr[] , int target)
    {
    HashMap map = new HashMap();
    for(int i=0;i

  • @gersonlopes1471
    @gersonlopes1471 Год назад

    Great job. Thanks.

  • @Raj10185
    @Raj10185 Год назад

    Understood Everything Striver:)

  • @samlinus836
    @samlinus836 11 месяцев назад +1

    As usual you rock as you explain 🔥
    Hey Raj, what if the array has duplicate elements
    for example,
    arr = [2,3,5,1,2] and target = 4
    Will hashing work for this case too?.................Because hashmap has unique keys

    • @ayushmittal9666
      @ayushmittal9666 10 месяцев назад +1

      Yes it will work beacuse we will check if the target - curr is present in the hashmap before putting the curr in map , So if the curr elem is duplicate of some element that is previiously present, we wont have to worry coz we are checking before inserting .

    • @samlinus836
      @samlinus836 10 месяцев назад

      @@ayushmittal9666 Understood thanks

  • @RajNamdev_19
    @RajNamdev_19 2 месяца назад +1

    Understood;

  • @abhijeetmishra3804
    @abhijeetmishra3804 Год назад

    understood bhaiya. u r best

  • @DevashishJose
    @DevashishJose Год назад

    Understood. Thank you.

  • @zameeralucknowi872
    @zameeralucknowi872 Год назад

    Thank u soo much Dada, u r the real inspiration. Respect++;

  • @konankikeerthi
    @konankikeerthi 2 месяца назад

    Understood bro.. thank you

  • @PulkitBhardwaj-ll2cf
    @PulkitBhardwaj-ll2cf Месяц назад

    Take a look at my solution.. with time complexity O(n) and space complexity O(1)..
    int n=nums.size();
    int i=0,j=1;
    while(j

  • @rhapsody4025
    @rhapsody4025 Год назад +1

    12:36
    sorting the array itself will take O(n*n)
    then another O(n) for the 2 pointer traverssal whick adds up to O(n*n) ruining the whole point of optimisation !

    • @venkatesh_kens
      @venkatesh_kens Год назад +1

      No bro when we sort using bubble sort or selection sort or insertion sort we will get O(n*2) but if we use MERGE SORT or QUICK SORT we will end up with O(N*log(N)) that’s what he is saying to do

    • @venkatesh_kens
      @venkatesh_kens Год назад +1

      But in this question we need to use only QUICK SORT but not MERGE SORT becoz MERGE SORT space complexity is O(N) but for Quick Sort it is O(1)

    • @venkatesh_kens
      @venkatesh_kens Год назад

      And you can see him saying it at 16:51

    • @balajik7376
      @balajik7376 4 месяца назад

      Got the same doubt

    • @balajik7376
      @balajik7376 4 месяца назад +1

      ​@@venkatesh_kensyou cleared it thanks❤

  • @ybs383
    @ybs383 9 месяцев назад

    The Last Optimal Solution of Sorting an array and using two pointer technique will be difficult to use if Array is not Sorted and I have been told to return index of the pairs.

  • @Manipinnaka
    @Manipinnaka 21 день назад

    i got an small logic striver not so optimal
    for(int i = 0; i < nums.length; i++) {
    for(int j = i + 1; j < nums.length; j++) {
    if(target-(nums[i]+nums[j])==0){
    return new int[]{i,j};
    array:[8,4,6,12[ }
    ex: target =14
    14-(8+4)=!0x
    14-(8+6)=0 true so we wil return the indexes

  • @chidambarjoshi3470
    @chidambarjoshi3470 Год назад +1

    I did not understand why you did mpp[a]=i at the last in the type-1 problem

  • @KR_Technical-hj3bf
    @KR_Technical-hj3bf 2 месяца назад

    I have to do clap for your optimal solution

  • @theprofessor2801
    @theprofessor2801 Год назад +1

    Thanks a lot

  • @dhruvmishra3411
    @dhruvmishra3411 11 месяцев назад

    Best explanation

  • @AniketKumar-hf2bo
    @AniketKumar-hf2bo 7 месяцев назад

    understood ,thank you

  • @udaytewary3809
    @udaytewary3809 Год назад

    Understood bhaiya 🙏 ❤️

  • @anantadevraj8445
    @anantadevraj8445 11 месяцев назад

    understand fully...😄

  • @RamanKumar-er8ie
    @RamanKumar-er8ie Год назад +1

    it does not work on tc=-3,-2,2,3,3 any please help me out in this

  • @shorts_shots514
    @shorts_shots514 Год назад

    understood !!
    thank you

  • @saibunny1253
    @saibunny1253 Месяц назад +1

    thank you anna

  • @behnamaslanirokh8799
    @behnamaslanirokh8799 3 месяца назад

    thanks bro that was perfect

  • @soumyadeepchatterjee2189
    @soumyadeepchatterjee2189 3 месяца назад

    For the 2 pointer approach, if we are sorting the array it is changing the original indexex. So, it is giving a error in LeetCode.

  • @vinitjain7
    @vinitjain7 9 месяцев назад +1

    for optimal solution why cant we return the pointer values as indexs ?? for variety 2

    • @saumitrapandey3070
      @saumitrapandey3070 9 месяцев назад +1

      because after sorting the array indexes are changed

  • @utsavseth6573
    @utsavseth6573 Год назад

    As usual lovely.

  • @faizanahmed9304
    @faizanahmed9304 Год назад

    striver bhaiya, thank you so much!!

  • @thesalahrand
    @thesalahrand 10 месяцев назад +1

    Understood

  • @torishi82
    @torishi82 Месяц назад

    Samaj aa gaya!!

  • @Radhika-0110
    @Radhika-0110 27 дней назад

    Understood💌

  • @raghavsinghal22
    @raghavsinghal22 Год назад

    Amazing bro!

  • @rupalikumari8829
    @rupalikumari8829 2 месяца назад +1

    Understood : )

  • @faysalmahmud2721
    @faysalmahmud2721 Год назад

    best explaination

  • @GhostVaibhav
    @GhostVaibhav Год назад +1

    Understood 🔥

  • @abhaynarware2423
    @abhaynarware2423 9 месяцев назад

    Striver OP🔥🔥🔥🔥

  • @culeforever5408
    @culeforever5408 10 месяцев назад +1

    understood

  • @ronakparjapati2264
    @ronakparjapati2264 3 месяца назад +1

    bhaiya Third approach will not be apply if we return index of both element because if we do sort the element then automatically element indexes will be changed...

  • @harsh0137
    @harsh0137 29 дней назад

    Understood 😅