How to Use the Two Pointer Technique

Поділитися
Вставка
  • Опубліковано 28 січ 2025

КОМЕНТАРІ • 62

  • @Ny_babs
    @Ny_babs 10 місяців тому +32

    If you cant explain it to a 3rd grade student then you dont understand it. This guy gets it! Great tutorial!

  • @solmazkhosravi4355
    @solmazkhosravi4355 7 місяців тому +13

    I don't usually leave comments but your explanation is so on point. Not too much not too little. Well done! I subscribed to the channel just by watching this video

  • @brayancuenca6925
    @brayancuenca6925 Рік тому +10

    So glad I ran into your channel; your way of making concepts visually understandable is a skill in its own right.

  • @charlesopuoro5295
    @charlesopuoro5295 2 роки тому +11

    Thank you so much for explaining this concept in a clear manner. It was evident that you made an effort to communicate and impart knowledge. And yeah, I sure did learn. Thanks again for this charitable endeavor. Salute!!!

  • @lucassmith6320
    @lucassmith6320 2 роки тому +6

    So for the first example the two sum, would this one only applied to sorted array?

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

      yes, because the elif sum < targetValue: pointerOne += 1 will not work on an unsorted array

  • @shashankl8589
    @shashankl8589 2 роки тому +65

    two pointers only work ,if it is a sorted array

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

      yup

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

      exactly!

    • @ragama623
      @ragama623 7 місяців тому

      Wrong. U can use two pointers in a non sorted array. Just look at leetcode 11

    • @muzikjay
      @muzikjay 8 днів тому

      That’s not true. It also works for the Trapping Rain Water problem, which does not use a sorted array.

  • @asadabbas5711
    @asadabbas5711 Рік тому +5

    thankyou for explaining in a very easy manner that even a newbie to programming can understand the problem and its solution pretty well. Although I've two questions regarding the two problem examples you discussed.
    1) do two pointer technique on TwoSum only work on a sorted array? I don't see accurate results on unsorted arrays.
    2) in your problem # 2, will your solution work on a link list with a cycle hoping for more than 1 node?

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

    Good job on your presentation! This helped me understand the basic two-pointer concept. My only suggestion would be to make a linked follow "suggested video" discussing what a "cycle" is. You are referring to this "cycle" as if it is a specific concept or data structure, as opposed to base/generic understanding of a cycle being anything that repeats in circular manner. Saying something like "A cycle in Python [or whatever language this is] is where you ping a database connection multiple times until you get a handshake. This can be the source of many headaches and hard to troubleshoot" ... Something like that. I just made that up, but you would of course give the actual explanation of what you are calling a #cycle AND a quick synopsis of why we care about finding one.

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

    Amazing explanation, very clean and concise. Keep up the great work!

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

    Thanks for the video. I haven't come across the cycle problem yet, so it's good to know how it works for when I encounter it. Great explanations and visuals!

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

    Your explanation is so on point! Thank you for a great video.

  • @matthewrodriguez3377
    @matthewrodriguez3377 4 роки тому +4

    loved the video! thanks for helping me understand this better

  • @soundaryanattuva6559
    @soundaryanattuva6559 7 місяців тому +1

    Such a great explaination!

  • @Prince-ol5zk
    @Prince-ol5zk 2 місяці тому

    Hey Team AlgoDaily,
    You are a great teacher. I like that you take your time and explain things in detail. Question, I understand that traditional two pointer (the pointers are in opposite ends) require the array to the sorted for the traditional two pointer to work. How about the sliding window, a variation of the two pointers? Does sliding window require the array like data structure to be sorted?

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

    You are an amazing teacher thanks for this

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

    is there any reason why the while loop condition cant be while the sum of the two pointers != target ?

    • @MagicTre123
      @MagicTre123 10 місяців тому +1

      You aren’t guaranteed to find the sum in the input array so using that loop condition may result in an infinite loop.

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

    Absolutely excellent explanations. Thank you!

  • @KyanCox
    @KyanCox 26 днів тому

    Such a helpful video

  • @mdouet
    @mdouet 3 роки тому +24

    For Two Sum, wouldn't it be fewer loop iterations to use a HashMap to store each element's location in the array, then you could just do the following on each loop iteration:
    1. Get the difference between the current value and the target.
    2. Check to see if that value is already stored in the HashMap, if it is, we're done, so just return the current index, and the index stored in the HashMap.
    3. If it wasn't found in the HashMap, store the current value and the current index in the HashMap for future lookups (see step 2).
    4. If we've gone through each element and didn't find a match, there is no match.
    At worst you would be doing N iterations, it seems the worst case for the Two Pointer approach is greater than N iterations unless I'm missing something?

    • @realcandadatamil5785
      @realcandadatamil5785 3 роки тому +17

      HashMap is also a good solution. But what about the space complexity? We are using a HashMap so it will become O(N). So the two-pointer approach is better than using a compliment hashmap IMKP

  • @serverhunter
    @serverhunter 11 місяців тому

    Great video and example thank you

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

    Great video

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

    Good introduction!

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

    Commenting for the algorithm, goodjob!

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

    I think the two-pointer technique is the same concept I recently read about in Cracking the Coding Interview, called the 'Runner Technique.'

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

    Goat tutorial

  • @LM_1010
    @LM_1010 2 роки тому +4

    For the fast pointer to go back to node 3, i assume by default node 4 being last node has its "next" pointing to the previous node?
    Because if not, how will
    fast.next.next from node 3
    move fast pointer back to node 3?

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

    The program u showed for solving two sum doesn't work correctly as it fails to pass the second test on the leetcode platform because all combinations are not tried.

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

    How can you assume the array is ascending if you just increased or decrease

  • @Rob-J-BJJ
    @Rob-J-BJJ Рік тому

    thank you my brother this is a really good tutorial!

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

    Great video, thank you for the clear explanation :)
    My question is regarding using the 'two pointer' technique to check for cycles within the linked list, which you present towards the end of the video.
    Would using more pointers solve the question faster? So if we used 3 pointers, i.e, pointer 1 has a speed of x, pointer 2 -> 2x and pointer 3 -> 3x would we solve the question faster?

  • @sntshkmr60
    @sntshkmr60 3 роки тому +5

    Does two pointer technique requires the array to be sorted?

    • @Japan2is4awesome
      @Japan2is4awesome 3 роки тому +4

      Yes since you can only traverse one way for both pointers and theres an assumption that behind the pointer is either smaller or bigger, depending on where the pointer started.

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

      Yes definitely, it will work only if the array is sorted

    • @world.trekker
      @world.trekker 3 місяці тому

      Yes

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

    Nice content!

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

    Great Work!!!

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

    Thank you really you are so helpful instant subscribe

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

    nice video

  • @chrisrey2516
    @chrisrey2516 24 дні тому

    Thanks mang :)

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

    Your 1000th Sub !!

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

    Thank u so much.

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

    my mind was blown

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

    The goat

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

    Awesome!

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

    gave you a follow brah

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

    make more videos

  • @Human_Evolution-
    @Human_Evolution- 3 роки тому

    I am not smart enough to do this. I tried for 1 hour. Do not understand the results.

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

    i love you

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

    nah

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

    stop explaining the theory and get into the practical syntax because none of this works

  • @mmm-ie5ws
    @mmm-ie5ws 11 місяців тому

    awesome video.

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

    Great video