Binary Search tutorial (C++ and Python)

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

КОМЕНТАРІ • 329

  • @Errichto
    @Errichto  5 років тому +300

    I recommend solving binary search problems on Leetcode (leetcode.com/explore/learn/card/binary-search/). Don't read their templates though, they aren't perfect.
    Start with easy problems: leetcode.com/explore/learn/card/binary-search/125/template-i/951/, leetcode.com/explore/learn/card/binary-search/126/template-ii/947/, leetcode.com/explore/learn/card/binary-search/126/template-ii/949/.
    You can find C++ and Python codes to some problems in my GitHub repo, github.com/Errichto/youtube/tree/master/lectures/binary-search.
    Note that some problems aren't same with what I talked about during a lecture. For example, Find Peak Element in Leetcode seems harder because the function can have multiple peaks.
    Huge thanks to Reddy for making captions!

    • @earth20010
      @earth20010 5 років тому

      Thanks!

    • @swagatochatterjee7104
      @swagatochatterjee7104 5 років тому

      I was trying to apply that ans = mid template for finding the minimum element in a sorted array. Was I doing something wrong?

    • @jdhp3696
      @jdhp3696 4 роки тому

      Thank you for the wonderful video. It really help me understand the algorithm better. It will be really great if you can do more videos like this where you go over other data structures and algorithms that are most used especially in interviews.

    • @Sehyo
      @Sehyo 4 роки тому

      4:20 Since (L+R)/2 would overflow, shouldn't (L/2) + (R/2) work though?

    • @roufshaik3530
      @roufshaik3530 4 роки тому

      what would be the eps value in sqrt() function?

  • @kshitijpatil2019
    @kshitijpatil2019 3 роки тому +57

    The most important lesson I learned from this video, don't bother updating low/high conditionally, stick with only one binary search template (for 99% of the cases), and use a variable instead to store pivot position as per your condition. It makes every binary problem 10x easier for me.

  • @naughtyrishan
    @naughtyrishan 5 років тому +277

    Thanks so much for such videos. A lot of people struggle badly to be good at competitive programming. You make their life easy. Thanks again.

    • @musicfan1695
      @musicfan1695 4 роки тому +13

      adding to that, you are a very humble person as well.

  • @sarveshdubey9347
    @sarveshdubey9347 5 років тому +110

    Hi, Sarvesh from India
    Please keep making such Conceptual videos.

  • @sauravpaul4131
    @sauravpaul4131 5 років тому +95

    Best lecture on binary search implementation, thanks Errichto.

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

    that observation where we can generalize the problem to a prefix of Falses and a suffix of Trues was mindblowing. thank you so much!

  • @rajbapat5607
    @rajbapat5607 5 років тому +42

    Great video! I can finally write binary search without hesitating or wasting time debugging a +1/-1 issue.

  • @medievalogic
    @medievalogic 3 роки тому +6

    I finally understood the idea of "getting a better answer". Most binary implementation on the Internet look like hacky, with people incrementing or decrementing the mid by one to get a desired answer. Thanks a bunch.

  • @dharmanshah1239
    @dharmanshah1239 5 років тому +50

    Thanks errichto for the well prepared material and thanks Reddy for captions!!

  • @AniketKumar-xg3ky
    @AniketKumar-xg3ky 5 років тому +4

    It is so far the best binary search video I have watched on UA-cam,Thank You very much....

  • @TheGanesamurthi
    @TheGanesamurthi 5 років тому +7

    The best half hour that I spent in entire 2020

  • @dcodernz
    @dcodernz 5 років тому +7

    Thanks Errichto! This was by far the best explanation I've seen/heard of binary search! I thought I had understood it before but this has deepened my understanding profoundly. The length is also good. I think it would help us all a lot of you did more videos like this to really get a deep understanding of algorithms.

  • @SumitBadsara
    @SumitBadsara 5 років тому +8

    This is the actual binary search! Not just (L+R)/2 . Thanks for these examples. Keep making such videos. Love from India🇮🇳.

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

      Well it is correct but will give int overflow . So it better to be on safer side . Thats it .

    • @SumitBadsara
      @SumitBadsara 4 роки тому

      @@devendrasingh4776 Bruh! That was just an example 😂. I meant he is not teaching the concept. He is teaching practical implementation. Like after this, some people might see binary search with a totally different perspective.

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

      @@SumitBadsara exactly . I am that guy who has changed perspective. Now i think like more of finding that condition rather than blindly dividing into half. All thanks to this guy.. 😊

  • @shubhamagrawal2407
    @shubhamagrawal2407 5 років тому +6

    Thanks Errichto, after watching this video now I have a better understanding of binary search and its applications.

  • @rohan8arora
    @rohan8arora 5 років тому +2

    All the things I learnt over weeks in a small 30 min video, thanks errichto!

  • @franceshe1350
    @franceshe1350 5 років тому +7

    Thank you! This is one of the best algorithm video with clarity and great explanations I saw so far! I learned so much, plz keep update these algorithm videos, maybe the more general ones that people used everyday. I think that might have a bigger impact for the general public :)

  • @Nyquiiist
    @Nyquiiist 3 роки тому +1

    Hands down the best explanation I've come across on BS yet. Looking at it from a a PoV of prefixes and suffixes was really eye opening, and has made solving some problems suuper easy. Thank you!!!

  • @srikanthkoraveni8210
    @srikanthkoraveni8210 5 років тому +2

    Thank you very much. Please continue to make videos covering the fundamentals. Loved the clear, concise and comprehensive presentation.

  • @codepractices7
    @codepractices7 5 років тому +2

    Thank you so much errichto! Please continue to make such comprehensive tutorials!!

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

    Thank you Errichto for the top-notch pedagogical content. Your techniques in this video helped me in one particular question in a technical interview for Amazon. I didn't receive the final offer, however it was a great experience thanks to you!

  • @BGivo
    @BGivo 5 років тому +2

    That was great, thank you. More videos like this, where you introduce a concept, and then provide related examples, would be amazing.

  • @dominikdabrowski7082
    @dominikdabrowski7082 4 роки тому

    I was really struggling with binary search, but your video made all the difference, thanks Kamil!

  • @de_naenae
    @de_naenae 3 роки тому +1

    Very intuitive explanations. I have never come across keeping an "ans" variable but it makes so much sense. Much easier to understand than other solutions!

  • @gupta_samarth
    @gupta_samarth 5 років тому +5

    Really awesome lecture. Thanks a lot for all the time you put into preparing this stuff. All those hours of BPS surely do pay off. Can't wait to learn more things from you.

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

    This channel is a goldmine!
    Glad I found this before getting my cs major

  • @prabhatsharma284
    @prabhatsharma284 5 років тому +1

    You cannot find a lecture on binary search better than this!

  • @iamtrash288
    @iamtrash288 4 роки тому +1

    Thank you for the comprehensive explanation of binary search! I really didn't know it was so powerful before!

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

    This is by far the best video for binary search. Kudos to your amazing work, Errichto!

  • @jannoszczyk5132
    @jannoszczyk5132 5 років тому

    Właśnie znalazłem twój kanał po obejrzeniu twojego wywiadu z Joma.
    Bardzo dokładnie i prosto wszystko tłumaczysz. Zdecydowanie najlepszy kanał z algorytmami jaki dotychczas widziałem!! Keep it up!

  • @alexneagu4466
    @alexneagu4466 5 років тому +1

    Congrats, Kamil, for a great video! With every video i learn more and more. Please make more videos like this, where you explain other popular algorithms. (segment trees, graph theory, string algoritms like KMP, etc.)

  • @kpopisthebestful
    @kpopisthebestful 5 років тому +10

    You should do more of these lectures, like sorting algos and data structures

  • @lpdc9767
    @lpdc9767 5 років тому +1

    Thanks so much for the clear and concise material! I hope you keep making these.

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

    Great video! You helped better understand binary search, but more importantly, its implications. You are a great teacher. Thank you.

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

    I feel this is the best binary search visualisation and strategy explanation because one thing is correct, it is a simple algorithm but at the same time it is very easy to write a buggy code for it.
    Thank you

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

    This is the best lecture on binary search. Thank you Errichto

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

    Usually really smart people like u are not known for their great teaching skills...
    U really changed that!

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

    This was great. Helped me learn a lot. I would be looking forward to more such sessions.

  • @shroud3354
    @shroud3354 5 років тому +7

    World class video must watch everyone👍

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

    Thanks a lot man! I was struggling to get a hold of all the variations. You made it so simple!

  • @davidjiang7929
    @davidjiang7929 4 роки тому

    Thanks for sharing! Your explanations are clear and concise. Also congrats on recent win!

  • @namanbudhiraja6756
    @namanbudhiraja6756 4 роки тому

    Hey Errichto, after watching this video , i have solved my all doubts related to binary search.Thanks for the amazing explanation.Love from India

  • @darshantawte7435
    @darshantawte7435 4 роки тому

    Best video on binary search I have seen till now

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

    Thanks for the great lecture! Instead of a template, you gave me a proper way to think of the algorithm.

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

    I loved your lecture, as a teacher/trainer of coding, you are best. Thanks man.

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

    This year I was studying mathematics on university Leiden and I dont know but my interest to learn mathematics declined so much just by attending school. A month ago I dropped out and found your videos and now I fell in love with competitive programming. Thank you so much!

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

    what a brilliant idea of thinking binary search problems as elements of search space being T or F !

  • @shroudyboi5673
    @shroudyboi5673 4 роки тому

    Wow he has explained the concept of discrete binary search beautifully. The topcoder tutorial was good but this video covers the topic really well

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

    Best explaination of binary search in youtube. Thanks a lot!

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

    Excellent way to cover Binary Search using a systematic way.. Thanks a lot

  • @nayanchudasama2014
    @nayanchudasama2014 4 роки тому

    What a great lecture, now binary search is Perfectly clear.
    Thanks a lot sir

  • @blu5037
    @blu5037 5 років тому +1

    Wow, super great video. Thanks for making this errichto.

  • @Sean-jv6bd
    @Sean-jv6bd 4 роки тому

    Hi Errichto this is an amazing lecture! Thank you so much and please keep up the great work!

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

    it was very good, I thought I new binary search but after watching this video I found out much more. It was really helpful. Thank you

  • @kairatopa9564
    @kairatopa9564 4 роки тому +1

    Thanks man u made my interview preparation much easier

  • @sanddevelopers
    @sanddevelopers 5 років тому +10

    Thanks a lot for the clear explanation

  • @KarthikKarthik-el5hh
    @KarthikKarthik-el5hh 2 роки тому

    thank you so much errichto, ur explanation was awesome
    now i understand binary search better

  • @Rich65501
    @Rich65501 5 років тому

    Excellent -- thanks for the correct way to calculate the midpoint in binary search.

  • @priyanandshukla8388
    @priyanandshukla8388 4 роки тому

    One of the best lectures on binary search

  • @lovleshbhatt7797
    @lovleshbhatt7797 4 роки тому +1

    need second part of this lecture as soon as possible , its a humble request from the fan side

  • @rahulsbhatt
    @rahulsbhatt 4 роки тому

    This is legit the best binary search video anyone has made 👏

  • @saitama-fv6my
    @saitama-fv6my 2 роки тому

    I watched your video and I got explained my topic what I wanted for. thank you so much sir 🙏

  • @mehedihasanjoy8726
    @mehedihasanjoy8726 5 років тому +34

    Pls do a leture on basic greedy and DP

    • @Errichto
      @Errichto  5 років тому +29

      I'm planning a lecture on dp, actually. It should be out soon.

  • @SushantKumar-iy4is
    @SushantKumar-iy4is 4 роки тому

    Well explained, Errichto. Clean and Simple.

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

    This is so awesome! Thank you so much @Errichto!

  • @rajansaharaju1427
    @rajansaharaju1427 5 років тому +13

    It's really safe zone but didn't think before. Thanks. L + (R-L)/2 = (2L + R - L) /2 = (L + R)/2

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

      I think 2L and L+R will overflow

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

      @@GreatKrish1 they were probably demonstrating how L + (R-L)/2 and (L + R)/2 is the same but does not overflow

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

      @@zafirhasananogh2421 Yeah that's what I wrote

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

    I have lost from rotated array, till then it was awesome!

  • @icenberg5908
    @icenberg5908 4 роки тому

    Thanks errichto for your efforts and clear presentation.

  • @cccctainvista
    @cccctainvista 4 роки тому

    Great video, love it, you are very good at making complex problems seem very simple!

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

    greetings from Mexico! those videos are really cool, thanks for all the effort put on those.

  • @amritbhardwaj99
    @amritbhardwaj99 4 роки тому

    This is a MUST watch vid on binary search!!

  • @__-to3hq
    @__-to3hq 5 років тому +11

    "The study from 1988 showed that only one in four textbooks has a correct implementation..."

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

    In the sorted rotated array variation. It's safe to say that assume false when element at mid is greater than "or equal to" the last element. Only greater than would fail for the case when the smallest number is repeated. Eg. 6, 7, 9, 9, 15, 19, 2, 2, 3

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

    Let's keep it binary with 2 words: PURE GOLD!

  • @pradiptarakshit35
    @pradiptarakshit35 4 роки тому +5

    Please, do a lecture on Graph Algorithms. Also, thanks for this video.

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

    Covers a lot of important observation and pattern in binary search.

  • @abhishekdhok5245
    @abhishekdhok5245 3 роки тому +1

    CP God for a reason.. Love your efforts. Love from India

  • @omti
    @omti 5 років тому +2

    Very cool content! Thank you!
    Waiting for more videos...

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

    Lovely classic lecture , mesmerized by explanations , thanks alot ❤️

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

    Thank you very much Errichto, really appreciate it. ❤️

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

    Your ways so easy to understand. thank you Eric so much!!!

  • @darshpatel8385
    @darshpatel8385 4 роки тому

    This video was really helpful to me. Thanks for making such good content.

  • @koocheukkeijacky9704
    @koocheukkeijacky9704 5 років тому +2

    Really appreciate of your videos !! very clear explanation

  • @laskdkmgful
    @laskdkmgful 5 років тому

    Best lecturer I have seen. Thanks!

  • @avisekshaw4748
    @avisekshaw4748 4 роки тому

    Great work bro. Thank you for uploading such a great content.

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

    Just clicked before thinking let's watch something might come up .

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

    Best lecture on binary search

  • @SuperArjun11
    @SuperArjun11 5 років тому +22

    This was great. Will you be covering data structures/algorithms not typically found in undergrad books? Square root decomposition and seg trees for example.

    • @ajr1791ze
      @ajr1791ze 5 років тому +4

      yes, i also say so.

    • @Errichto
      @Errichto  5 років тому +23

      Yes, I plan both sqrt deco and seg trees :) More video ideas here: github.com/Errichto/youtube/wiki/Video-ideas

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

      @@Errichto Please make a video on segment and fenwick trees.

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

    thanks a lot enrichto!! beautifully explained!!

  • @beanlighter9491
    @beanlighter9491 4 роки тому

    Thank you so much sir. Countering problems with that T...F was quite interesting I will try sometimes

  • @yilinma8367
    @yilinma8367 4 роки тому

    I wish I watched this video earlier. The True & False pattern is so helpful!!!

  • @areebwadood6273
    @areebwadood6273 5 років тому +1

    Great learned some new applications of binary search

  • @abdurrahaman388
    @abdurrahaman388 4 роки тому

    Most informative binary search tutorial ❤️

  • @rajgarg8883
    @rajgarg8883 5 років тому +5

    Thanks, Please make videos on educational round problems.

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

    Awesome video lad, great explanations!

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

    Everyone else: Use (l+r)/2 to find the middle element. No problem at all.
    Errichto: Never use this formula xD

  • @teezp12490a
    @teezp12490a 5 років тому

    Thanks for the video! Came here from Joma's vid

  • @utkarshsharma424
    @utkarshsharma424 4 роки тому

    Man You are the best Teacher !!

  • @barakode414
    @barakode414 4 роки тому

    Thank you very much. It's easy after watching your video.

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

    Trying desperately to wrap my head around rotated, sorted arrays haha. Anyways great video!

  • @ajr1791ze
    @ajr1791ze 5 років тому +2

    amazing lecture. Thanks !