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

Поделиться
HTML-код
  • Опубликовано: 31 июл 2024
  • To see more videos like this, you can buy me a coffee: www.buymeacoffee.com/studyalg...
    Two Sum is a programming question on LeetCode and very important for interviews. You are given an array of integers and a target value as the input. There are exactly two integers in the array which add up to the target value. You are required to return the indices of these 2 integers. Watch this video to learn 3 ways of solving this problem. You will learn a brute force solution, a solution using sorting, and a solution using Hash-map. Each of the solution has been explained with step by step animations and with a dry-run of the code so that it sticks in your mind forever.
    00:00 - Intro
    01:07 - Problem statement and description
    03:54 - Brute Force Method
    06:03 - Method 1: Using Sorting
    10:36 - Method 2: Using Hash-Map
    14:04 - Dry-run of code
    16:35 - Final thoughts
    📚 Links I talk about in the video:
    Brute Force Method: • Brute Force algorithms...
    Quick Sort: • Quick Sort super easy ...
    Time Complexity: • What is the Time Compl...
    Actual problem on LeetCode: leetcode.com/problems/two-sum/
    Code on Github: github.com/nikoo28/java-solut...
    Test cases on GitHub: github.com/nikoo28/java-solut...
    📘 A text based explanation is available at: studyalgorithms.com/array/lee...
    🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
    💻 Get Social 💻
    Follow on Facebook at: / studyalgos
    Follow on Twitter at: / studyalgorithms
    Follow on Tumblr at: / studyalgos
    Subscribe to RSS feeds: studyalgorithms.com/feed/
    Join fan mail: eepurl.com/g9Dadv
    #leetcode #programming #interview

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

  • @nikoo28
    @nikoo28  3 года назад +41

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

  • @halimaomar9820
    @halimaomar9820 2 года назад +36

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

    • @nikoo28
      @nikoo28  2 года назад +3

      Happy that I could help :)

  • @wenkaiyang1487
    @wenkaiyang1487 8 месяцев назад +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

  • @bobbysokhi7296
    @bobbysokhi7296 7 дней назад +1

    Underrated channel.

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

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

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

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

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

    Love the way you teach, TYSM☺

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

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

  • @1tav0
    @1tav0 2 года назад +1

    Thank you for the video it really helped me!

  • @dineshpatil6698
    @dineshpatil6698 2 года назад

    such clear explantion , subscribed

  • @kishanbhadoriya8701
    @kishanbhadoriya8701 6 месяцев назад

    Thank you very much, finally understood

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

    awesome explanation..keep teaching us

  • @sarthakgadge5223
    @sarthakgadge5223 18 дней назад

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

  • @anchalsoni8614
    @anchalsoni8614 3 года назад

    awesome video... keep making them

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

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

  • @ayushtalks2.0
    @ayushtalks2.0 Год назад +8

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

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

      Thank you so much for your support :)

  • @sanjeewagunarathna1990
    @sanjeewagunarathna1990 2 года назад

    Great video best explanation

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

    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  6 месяцев назад +1

      thanks for the appreciation and motivation :)

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

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

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

      Thanks a ton

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

    Thank you ❤

  • @04.nehalsingh12
    @04.nehalsingh12 2 года назад

    awesome tutorial sir

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

    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  9 месяцев назад +1

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

  • @TV-wk7oo
    @TV-wk7oo Год назад

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

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

    Very smooth sir

  • @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 :)

  • @Rockstar-16
    @Rockstar-16 Год назад +3

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

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

      Thank you so much for your kind words.

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

    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"

  • @manjunathakaliwalLife_Atlanta
    @manjunathakaliwalLife_Atlanta 6 месяцев назад +1

    Good one

  • @aniketpurohit7601
    @aniketpurohit7601 6 месяцев назад +1

    Amazing

  • @shacks-b3b
    @shacks-b3b 6 месяцев назад

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

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

      More to come!

  • @AbhishekRamola-u7y
    @AbhishekRamola-u7y Месяц назад

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

  • @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 :)

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

    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  11 месяцев назад

      so happy to read this

  • @jst8922
    @jst8922 6 месяцев назад

    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.

  • @TeluguAbbai802
    @TeluguAbbai802 6 месяцев назад +1

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

    • @nikoo28
      @nikoo28  5 месяцев назад +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.

  • @IorgaArtStudio
    @IorgaArtStudio 3 года назад

    subbed!

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

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

    your explanation is for more better than other youtuber

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

      Glad you feel that way :D

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

    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  2 месяца назад

      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 😄

  • @ganesang7217
    @ganesang7217 8 месяцев назад

    its cool

  • @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? :)

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

    Is this possible using binary search?

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

    Best

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

    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 ?

  • @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.

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

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

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

      GoodNotes 6

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

    bro plz do in python

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

    Awesome channel! Why I Find it so late?

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

      Glad I could help you 😄

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

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

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

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

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

      @@nikoo28 thanks.

  • @karthik-varma-1579
    @karthik-varma-1579 3 месяца назад

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

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

      the optimized method takes care of all different test cases

    • @karthik-varma-1579
      @karthik-varma-1579 3 месяца назад

      @@nikoo28 Ohk Thanks Sir

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

    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  Год назад

      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 Месяц назад

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

  • @user-eu8bd9ul3q
    @user-eu8bd9ul3q 7 месяцев назад

    sir
    what language you are used here

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

    100

  • @YNKeerthi-ry6jx
    @YNKeerthi-ry6jx 3 месяца назад +1

    koko eating banana 875 leetcode question

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

      It is available now: ruclips.net/video/JGYXNpZaW2U/видео.htmlsi=2ftslOTD1LCE8-r2