Two Sum (LeetCode #1) | 3 Solutions with animations | Study Algorithms

Поділитися
Вставка
  • Опубліковано 15 жов 2024

КОМЕНТАРІ • 99

  • @nikoo28
    @nikoo28  3 роки тому +49

    At 12:58, I am supposed to add "8" to the hash table and not "11".
    Sorry for the error.

    • @atharvalaad8926
      @atharvalaad8926 2 місяці тому

      😅😅😅😅😅😅😅 15:52 😅😅😊😅😅😅 15:52 15:52 15:52 15:52 15:52 😅 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 😅 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 15:52 😅 15:52 15:52 15:52 15:52 15:52 15:52 15:52

  • @halimaomar9820
    @halimaomar9820 2 роки тому +46

    This is absolutely the best explanation on UA-cam. Everyone else goes straight to coding and doesn't break down the logic beforehand.

    • @nikoo28
      @nikoo28  2 роки тому +3

      Happy that I could help :)

  • @AKASH-fe6vy
    @AKASH-fe6vy 22 дні тому +1

    So nice of you to put this in a simple yet insightful manner, thanks a lot.

  • @billyfigueroa1617
    @billyfigueroa1617 7 місяців тому +4

    Excellent content. This is the second video I watch which allows one to be able to devise a nicer solution simply by sorting the array. The hash map solution is genius and is not complicated
    I will now, always look at a problem and find out how I can solve it by first thinking "Would sorting this array help to simplify the solution"

  • @ayushtalks2.0
    @ayushtalks2.0 2 роки тому +8

    One of the best way of teaching . Sir Keep posting more videos of dsa. 👍👍

    • @nikoo28
      @nikoo28  2 роки тому +2

      Thank you so much for your support :)

  • @wenkaiyang1487
    @wenkaiyang1487 11 місяців тому +3

    thank you soooo much! I checked out so many videos about this question. finally your video helped me out! firmly subscribed to your channel

  • @Spear_of_Boudicca
    @Spear_of_Boudicca 21 день тому

    Good video, very similar to my university lectures.
    I think the reason a lot of people fail to learn some things on their own is that they just see code and want to understand it magically, you need to get the logic behind stuff.

  • @aman_rockstar31
    @aman_rockstar31 2 місяці тому

    you r really an excellent teacher one thing i want to tell ur explanation is better that striver thanks for providing such videos

  • @Karan-ow4wl
    @Karan-ow4wl 14 днів тому

    Best Video Explanation so far !!!

  • @Rockstar-16
    @Rockstar-16 2 роки тому +3

    Sir😄, I have no words, I have never seen anyone explain this nicley, thank you so much❤

    • @nikoo28
      @nikoo28  2 роки тому

      Thank you so much for your kind words.

  • @bobbysokhi7296
    @bobbysokhi7296 2 місяці тому +1

    Underrated channel.

  • @harihardhik3293
    @harihardhik3293 Рік тому +1

    Such a great way of explaining this problem. You have redefined teaching sir!!! Amazing Work!!!!

  • @yowanehakuuu
    @yowanehakuuu Рік тому

    I watched two other videos before coming here , to be honest you are the best one . I only understand from you because other people talk too long without explaining they talk about the problem not the solution. You even taught me the math side of this . Thank you , I subscribed. I will watch the rest of the solutions

    • @nikoo28
      @nikoo28  Рік тому +1

      I prefer to focus on the problem. Language is a tool anyway, and they will keep changing and evolving. Thanks for the sub!!

  • @shacks-b3b
    @shacks-b3b 8 місяців тому

    Please keep uploading DSA/leetcode videos, they all are really helpful, thanks!

    • @nikoo28
      @nikoo28  8 місяців тому

      More to come!

  • @sahilhansofficial
    @sahilhansofficial Рік тому +3

    Love the way you teach, TYSM☺

  • @pooja3711
    @pooja3711 9 місяців тому

    you are the best.I really appreciate your hard work and step-by-step approach to how to overcome with a solution. Hats off

    • @nikoo28
      @nikoo28  9 місяців тому +1

      thanks for the appreciation and motivation :)

  • @elizabeth00653
    @elizabeth00653 4 місяці тому

    Thank you for taking the time to create this video and make a clear explanation

  • @sarthakgadge5223
    @sarthakgadge5223 3 місяці тому

    Thanks a lot, I was able to understand this problem thanks to your efforts.

  • @brahmithegod9558
    @brahmithegod9558 2 роки тому +1

    I was searching for the solution but do not found dryryn but u did it thank bro ❤️

  • @KaisarAnvar
    @KaisarAnvar Рік тому +3

    You're by far the most detailed explainer I've seen. I've seen a few, but you're really amazing. Having a coding interview soon.

    • @nikoo28
      @nikoo28  Рік тому +1

      All the very best for your upcoming interviews :)

  • @coldcases_2
    @coldcases_2 21 день тому +2

    sir @nikoo28 ,there is a mistake in two pointer approch i think where if => num1 and num2 are the same (e.g., nums = [3,3], target = 6), so code will return the same index for both numbers, especially if there are duplicates in the original array. for ex nums =
    [3,3]
    target =
    6
    Output
    [1,1]
    Expected
    [0,1] which is wrong so should we apply something else here?

    • @matthewzarate8851
      @matthewzarate8851 День тому

      I had a similar issue with this test case as well.
      The way I fixed it is I made sure I was only inserting a {key:value} pair into my hashMap (example: target - nums[index]) at the end of the for-loop. I evaluate if condition ( map.containsKey(target - nums[index]) ) first, if not satisfied then I insert this new {key:value} pair into my hashMap and continue on with the next iteration of the for-loop. This generalizes the solution to fix the [3,3] target = 6 case.
      Also I haven't tested Nikhil's solution but it looks correct to me.

    • @coldcases_2
      @coldcases_2 День тому

      @@matthewzarate8851 i was talking about two pointer approch there is mistake in two pointer approch i already give example in my comment

    • @matthewzarate8851
      @matthewzarate8851 22 години тому

      @@coldcases_2 There isn't a mistake in the two pointer approach. You need to add complexity for the case when i = j, that takes care of this special case.

    • @matthewzarate8851
      @matthewzarate8851 22 години тому

      @@coldcases_2 when doing the brute force method you can fix this condition by
      for () {
      for() {
      if (target == nums[I] + nums[J]) && I != J then return (I,J);
      }
      }
      This takes care of all test cases. We added some complexity in the if condition.

  • @joshuajacobs6855
    @joshuajacobs6855 Рік тому

    Only just got into coding a few days ago coming from a teacher background. Great explanations!

  • @RajKumar-li8dc
    @RajKumar-li8dc Рік тому +1

    Best way to teach sir plz discuss more question and make a proper playlist ❤❤❤

    • @nikoo28
      @nikoo28  Рік тому +1

      Check out my playlists in the channel :)

  • @girdharipatel2
    @girdharipatel2 Рік тому

    your explanation is for more better than other youtuber

    • @nikoo28
      @nikoo28  Рік тому

      Glad you feel that way :D

  • @kishanbhadoriya8701
    @kishanbhadoriya8701 9 місяців тому

    Thank you very much, finally understood

  • @TV-wk7oo
    @TV-wk7oo Рік тому

    Thank you, you are the best! Visualizing the code helped me a lot

  • @japaneseger
    @japaneseger 2 роки тому

    You explained it very well, best video out there regarding this solution!!"!!!!

  • @bivinnandhu781
    @bivinnandhu781 Рік тому

    Thank you bro you do grate things
    your explanation is very simple and easy to understand
    Iam so happy to see this video

    • @nikoo28
      @nikoo28  Рік тому

      so happy to read this

  • @AbhishekRamola-u7y
    @AbhishekRamola-u7y 3 місяці тому

    Kudos! Great explanation brother.P.s: Bro, you sound exactly like Pennywise from It.

  • @1tav0
    @1tav0 2 роки тому +1

    Thank you for the video it really helped me!

  • @malkitsaggu
    @malkitsaggu 2 місяці тому

    Best explanation 👌

  • @dineshpatil6698
    @dineshpatil6698 2 роки тому

    such clear explantion , subscribed

  • @sunnyrajesh
    @sunnyrajesh 5 місяців тому

    Hello I have a small doubt ? How we return two index values here map.get(complement),i);
    Can any one explain 🤔 does it return complement index and current I value ?

  • @206_dindayalpatra3
    @206_dindayalpatra3 6 місяців тому +1

    Thank you ❤

  • @juledoc_07
    @juledoc_07 Рік тому

    I had a question about the tastable, 19-4 is 15, but it is on the table so why don't we stop there? Thank you

  • @lipovgaming7245
    @lipovgaming7245 Місяць тому

    but how do you return 2 elements.

  • @pulastyadas3351
    @pulastyadas3351 2 роки тому

    awesome explanation..keep teaching us

  • @AkaNuclear
    @AkaNuclear 5 місяців тому

    For the second solution where we are using hashmap, the time complexity of finding an element is also O(n) in case of hash collisions

    • @nikoo28
      @nikoo28  5 місяців тому

      For a very big input size, yes there will be collisions…but for the domain of this problem you should be just fine.
      Good to discuss this with your interviewer though 😄

  • @kunalkheeva
    @kunalkheeva 2 роки тому

    thank you for a great explanation, what would be its space complexity?

    • @nikoo28
      @nikoo28  2 роки тому

      Which solution do you want the space complexity for? :)

  • @manjunathakaliwalLife_Atlanta
    @manjunathakaliwalLife_Atlanta 8 місяців тому +1

    Good one

  • @im_andre6dev56
    @im_andre6dev56 10 місяців тому

    Hi Nikhil, what program do you use to draw the solution?

    • @nikoo28
      @nikoo28  9 місяців тому

      GoodNotes 6

  • @prakharpathak5899
    @prakharpathak5899 Рік тому

    Very smooth sir

  • @aniketpurohit7601
    @aniketpurohit7601 9 місяців тому +1

    Amazing

  • @sanjeewagunarathna1990
    @sanjeewagunarathna1990 2 роки тому

    Great video best explanation

  • @shwetamandal6616
    @shwetamandal6616 Рік тому

    what is this """'' return new int[]{map.get(complement),i}; """" ? as it throws error and i am getting you've used curly brackets with new int[]...................please reply..........................

    • @nikoo28
      @nikoo28  Рік тому

      It gets the complement pair and then returns it as an array.

  • @dipanwitasahoo6643
    @dipanwitasahoo6643 6 місяців тому

    Is this possible using binary search?

  • @shubhi1396
    @shubhi1396 3 дні тому

    Why new is written in return

  • @anchalsoni8614
    @anchalsoni8614 3 роки тому

    awesome video... keep making them

  • @jst8922
    @jst8922 9 місяців тому

    06:03 - Method 1: Using Sorting
    This is basically sliding window solution, when you have two pointers , left and right and you move them correspondingly.

  • @sushant3530
    @sushant3530 2 роки тому

    Sir will 3rd Method will work if array has Duplicate elements . Becoz in HashMap , the key cannot be duplicate. And if array elements repeats , they duplicate key is found. PLZZZZ PLZZ EXPLIAN THIS SIR

    • @nikoo28
      @nikoo28  2 роки тому

      since the problem says you will have EXACTLY ONE SOLUTION, you do not need to worry about duplicate number. Because if a duplicate number exists, then you will have 2 possible solutions...and hence that is not a valid scenario.

    • @ashishkumaryadav7701
      @ashishkumaryadav7701 Рік тому

      @@nikoo28 but in the example there is repetitive element

    • @RapJunkie
      @RapJunkie 3 місяці тому

      @@nikoo28 we have a valid scenario [3,3], and this method won't work :(

  • @Programming-for-all-u2l
    @Programming-for-all-u2l 8 місяців тому +1

    I do not know why he gets very few likes and he is the best teacher for explaining the leetcode problems

    • @nikoo28
      @nikoo28  8 місяців тому +1

      so happy you believe in me. Please share the videos if possible. Trying my best with SEO to get ranked up in search results.

    • @ashishsonii
      @ashishsonii Місяць тому

      Brother don't stop making leetcode questions​@@nikoo28

  • @04.nehalsingh12
    @04.nehalsingh12 2 роки тому

    awesome tutorial sir

  • @karthik-varma-1579
    @karthik-varma-1579 6 місяців тому

    AT 10:31 the method is not working for negative numbers in the array;

    • @nikoo28
      @nikoo28  6 місяців тому +1

      the optimized method takes care of all different test cases

    • @karthik-varma-1579
      @karthik-varma-1579 6 місяців тому

      @@nikoo28 Ohk Thanks Sir

  • @kashmirviews4332
    @kashmirviews4332 5 місяців тому

    Can anybody tell how to write the main function code for the same function.

    • @nikoo28
      @nikoo28  5 місяців тому +1

      Just create an object of the class and call it in the main method

    • @kashmirviews4332
      @kashmirviews4332 5 місяців тому

      @@nikoo28 thanks.

  • @ME022GopinathanAsokan
    @ME022GopinathanAsokan 10 місяців тому

    sir
    what language you are used here

  • @ymhuang651
    @ymhuang651 2 роки тому

    Awesome channel! Why I Find it so late?

    • @nikoo28
      @nikoo28  2 роки тому

      Glad I could help you 😄

  • @Samtoosoon
    @Samtoosoon 4 місяці тому +1

    Best

  • @ashishsonii
    @ashishsonii Місяць тому

    Dont stop DSA questions

  • @IorgaArtStudio
    @IorgaArtStudio 3 роки тому

    subbed!

  • @ganesang7217
    @ganesang7217 10 місяців тому

    its cool

  • @powerball200
    @powerball200 Рік тому +1

    bro plz do in python

  • @abhinavd2
    @abhinavd2 Рік тому

    100

  • @YNKeerthi-ry6jx
    @YNKeerthi-ry6jx 6 місяців тому +1

    koko eating banana 875 leetcode question

    • @nikoo28
      @nikoo28  5 місяців тому +1

      It is available now: ua-cam.com/video/JGYXNpZaW2U/v-deo.htmlsi=2ftslOTD1LCE8-r2