Two Sum II - Two Pointers Algorithm - Leetcode 167

Sdílet
Vložit
  • čas přidán 25. 04. 2024
  • FAANG Coding Interviews / Data Structures and Algorithms / Leetcode

Komentáře • 70

  • @GregHogg
    @GregHogg  Před 3 měsíci +9

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

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

      int left=0;
      int right=numbers.length-1;
      int[] res=new int[2];
      while(leftnumbers[left]){
      left++;
      }else{
      right--;
      }
      }
      return res; this would be my code for this question i just wrote here i didnt submit the soultion

  • @not_vinkami
    @not_vinkami Před 3 měsíci +26

    I saw you using n as in r = n - 1 before declaring that n = len(numbers)
    Good job

  • @RS-fz4ps
    @RS-fz4ps Před 3 měsíci +4

    This is a great teaching question. I love it when an answer has a (more or less) optimal solution that uses two pointers.

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

      Yeah it feels very elegant indeed 😊

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

    Also, please send an email to greg.hogg1@outlook.com if you're interested in 1 on 1 sessions with me to learn data structures and algorithms :)

  • @hakan6449
    @hakan6449 Před 3 měsíci +9

    Would be great if you displayed the time complexity in the end

  • @muhammadkamil3558
    @muhammadkamil3558 Před 3 měsíci +7

    Why do we return l+1, r+1 and not l, r

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

      that’s a very good question

    • @nigh_anxiety
      @nigh_anxiety Před 3 měsíci +2

      He left it out of the description, but the problem also says its a 1-indexed array. So if you're coding in a 0-indexed language such as Python, C, JavaScript, etc, you need to adjust the indexes by 1.
      The problem actually says : "Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2."

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

      This. The dumb question asks for it +1 for some silly reason

  • @dheerajsharma5032
    @dheerajsharma5032 Před 3 měsíci +2

    Doing Great bro ❤

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

    if only two numbers, then extremely simple. In general, suppose we want to look for numbers, no matter how many, that have the summation equal to a given number k, then a fundamental way is to apply dynamic programming.

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

      Or just backtracking, yeah! :)

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

      @@GregHogg I assume they are the same thing? Practically, to perform DP, one has to do backtracking.

  • @md.shariarkabir7350
    @md.shariarkabir7350 Před 3 měsíci

    Binary search or Two Pointer : TC => O(nlogn) SC => O(1) (When Array not sorted)
    TC => O(n) SC => O(1) (sorted array)
    Hash Map: TC => O(n), SC => O(n)

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

      No no two pointer is o(n) time and o(1) space

    • @md.shariarkabir7350
      @md.shariarkabir7350 Před 2 měsíci

      @@GregHogg sorry i made a mistake, i think array was'nt sorted.
      You're right.

  • @user-df6vb9rn4c
    @user-df6vb9rn4c Před 21 dnem

    So the reason we used two point here is becoz this is sorted right? Not in two sum 1 as it wasn't sorted?!

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

    Quick question, does it work if you just have a dictionary(hashmap)? Like index through each of the element A[i] then store (target - A[i]) as the key and its index as the value. Then return the index if found in the hashmap. This is O(n) too I believe.
    P/s: if it need constant space then 2 pointer is the way to go.

    • @iron3561
      @iron3561 Před 3 měsíci +2

      If the array was not sorted, the hashmap approach would be O(N) and the two pointers approach would be O(N*Log(N)).

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

      You're super right, the point of this solution is to use constant space:)

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

      But the array is unsorted to begin with I think hashmap would be a better solution

  • @400elochess
    @400elochess Před 2 měsíci

    i would just do like put p1 on 0 then p2 from 1-len of list then if sum not found, p1 goes to 1 and p2 starts from 2-len of list, this continues until sum is found
    then if p1 == p2: return p1, p2
    works on both two sum and two sum II

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

      Does it? Did you test it?

    • @400elochess
      @400elochess Před měsícem

      @@GregHogg not yet, this is a O(n²) solution, so use a hashmap

    • @400elochess
      @400elochess Před měsícem

      this is like L: 0 R: 1-len(numbers)
      then L+1 if L+R is not target, repeat till found

    • @400elochess
      @400elochess Před měsícem

      also can you break down linked lists

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

    How does your code work if you declarated r before n if the declaration of r is dependent of n?

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

      It doesn't, whoops 😂

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

    great great

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

    too good

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

    Three sum is more interesting.
    You have a loop of index i and inside you solve with this TP algorithm to find j and k

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

      Yeah 3sum is definitely interesting with its intricacies around this and avoiding duplicates

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

    Do these companies still ask such easy questions?

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

      Sometimes! Nowadays they're leaning into more difficult ones though

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

    Why n is being used to define r before it is defined

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

    Suggestion: Pls pause for an extra couple of seconds after revealing your soln.

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

      Thank you. 60 second time limit so unfortunately it can get kinda rushed at the end. Sorry about that

    • @RS-fz4ps
      @RS-fz4ps Před 3 měsíci

      You can pause.

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

    It looks like a binary search what is difference between the br and tp

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

      Not a binary search!

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

      In BS, you're eliminating half the list with each iteration, resulting in O(logN) time.
      With TP, you're still iterating through the whole list, but with 2 pointers instead of one. This results in O(N) time if you're not nesting pointer iterations.

  • @ashrafel-gaaly8657
    @ashrafel-gaaly8657 Před 3 měsíci

    Why return l+1 and r+1 not l and r you found?

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

      Yeah just the stupid problem desired that for some reason

  • @masscantbecreatedordestroy8868

    could you still use a hash map on this one?

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

      You don’t really need hash map for this one. Try doing a dry run of this with a hashmap and you will notice right away that there is no use of it.

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

      That would still get you a solution, but it's less efficient, and the actual problem description on Leetcode says that your solution "must use constant space", althought it doesn't enforce that. Using a hashmap would be O(n) space, not O(1)

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

    *Ch. 1113
    not 1118

  • @12371eric
    @12371eric Před 3 měsíci

    Two sum 2 is easier than two sum 1 imo

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

      Yes quite arguably it is

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

    It's not an array 😭

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

    What if it’s unsorted?

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

      Then you use normal two sum

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

      You use two sum. Where you basically go and and get the first number add it with everything if no sum equals target then you go to de next number adding it with everything but skipping the first number

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

      @@Progamertoatoh that would n**2 time complexity or basically we can convert that normal two sum to this one by sorting making whole complexity as nlogn.
      Just thinking if we can do like AVL and work on some way to get answer in O(n)

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

      You'd use a Hashmap and check for each number if its complement (target - itself) is in the map

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

      @@GregHoggmake sense. That should be worst case o(nlogn) too if we assume hashmap has so much collision or we using balanced tree internally for hashmap thus eventually making it O(nlogn). Best case O(n)/ avg case O(n) definitely.
      How about three sum say finding triplet a+b+c = 0. Can we do something using hashmap?
      Maybe like this:
      Say b+c = k.
      Then find for a = -k.
      We can get all combinations of b and c as nc2 or O(n^2) and then find if -k exists in hashlist of a, just that we need to keep track of not using same element of b and c.
      This could be one way from two pointer but i feel using standard two pointer is much better