Binary Search tutorial (C++ and Python)

  Рет қаралды 264,961

Errichto Algorithms

Errichto Algorithms

Күн бұрын

Пікірлер: 328
@Errichto
@Errichto 5 жыл бұрын
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 4 жыл бұрын
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 жыл бұрын
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.
@maximus1172
@maximus1172 Жыл бұрын
Very insightful I must say
@naughtyrishan
@naughtyrishan 5 жыл бұрын
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 жыл бұрын
adding to that, you are a very humble person as well.
@sarveshdubey9347
@sarveshdubey9347 5 жыл бұрын
Hi, Sarvesh from India Please keep making such Conceptual videos.
@medievalogic
@medievalogic 3 жыл бұрын
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.
@rajbapat5607
@rajbapat5607 5 жыл бұрын
Great video! I can finally write binary search without hesitating or wasting time debugging a +1/-1 issue.
@sauravpaul4131
@sauravpaul4131 5 жыл бұрын
Best lecture on binary search implementation, thanks Errichto.
@AnkitJosh
@AnkitJosh 4 жыл бұрын
Period
@mujtabahussain7015
@mujtabahussain7015 3 жыл бұрын
@@AnkitJosh .
@joshiakhil007
@joshiakhil007 3 жыл бұрын
@@AnkitJosh comment
@TheGanesamurthi
@TheGanesamurthi 4 жыл бұрын
The best half hour that I spent in entire 2020
@dharmanshah1239
@dharmanshah1239 5 жыл бұрын
Thanks errichto for the well prepared material and thanks Reddy for captions!!
@nijikatherock
@nijikatherock 2 жыл бұрын
that observation where we can generalize the problem to a prefix of Falses and a suffix of Trues was mindblowing. thank you so much!
@AniketKumar-xg3ky
@AniketKumar-xg3ky 5 жыл бұрын
It is so far the best binary search video I have watched on KZbin,Thank You very much....
@shubhamagrawal2407
@shubhamagrawal2407 5 жыл бұрын
Thanks Errichto, after watching this video now I have a better understanding of binary search and its applications.
@rohan8arora
@rohan8arora 5 жыл бұрын
All the things I learnt over weeks in a small 30 min video, thanks errichto!
@Nyquiiist
@Nyquiiist 3 жыл бұрын
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!!!
@SumitBadsara
@SumitBadsara 5 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@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.. 😊
@prabhatsharma284
@prabhatsharma284 5 жыл бұрын
You cannot find a lecture on binary search better than this!
@dcodernz
@dcodernz 5 жыл бұрын
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.
@franceshe1350
@franceshe1350 5 жыл бұрын
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 :)
@srikanthkoraveni8210
@srikanthkoraveni8210 5 жыл бұрын
Thank you very much. Please continue to make videos covering the fundamentals. Loved the clear, concise and comprehensive presentation.
@shroud3354
@shroud3354 5 жыл бұрын
World class video must watch everyone👍
@dominikdabrowski7082
@dominikdabrowski7082 4 жыл бұрын
I was really struggling with binary search, but your video made all the difference, thanks Kamil!
@de_naenae
@de_naenae 3 жыл бұрын
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!
@codepractices7
@codepractices7 5 жыл бұрын
Thank you so much errichto! Please continue to make such comprehensive tutorials!!
@anweshbhattacharya8017
@anweshbhattacharya8017 4 жыл бұрын
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!
@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!
@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
@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
@pradyundevarakonda6849
@pradyundevarakonda6849 4 ай бұрын
This channel is a goldmine! Glad I found this before getting my cs major
@BGivo
@BGivo 5 жыл бұрын
That was great, thank you. More videos like this, where you introduce a concept, and then provide related examples, would be amazing.
@gupta_samarth
@gupta_samarth 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.
@iamtrash288
@iamtrash288 4 жыл бұрын
Thank you for the comprehensive explanation of binary search! I really didn't know it was so powerful before!
@adityarai8963
@adityarai8963 Жыл бұрын
This is by far the best video for binary search. Kudos to your amazing work, Errichto!
@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
@darshantawte7435
@darshantawte7435 4 жыл бұрын
Best video on binary search I have seen till now
@sameerbhatti363
@sameerbhatti363 3 жыл бұрын
Great video! You helped better understand binary search, but more importantly, its implications. You are a great teacher. Thank you.
@anands9407
@anands9407 4 жыл бұрын
Usually really smart people like u are not known for their great teaching skills... U really changed that!
@kpopisthebestful
@kpopisthebestful 5 жыл бұрын
You should do more of these lectures, like sorting algos and data structures
@nayanchudasama2014
@nayanchudasama2014 3 жыл бұрын
What a great lecture, now binary search is Perfectly clear. Thanks a lot sir
@rahulsbhatt
@rahulsbhatt 3 жыл бұрын
This is legit the best binary search video anyone has made 👏
@rajansaharaju1427
@rajansaharaju1427 5 жыл бұрын
It's really safe zone but didn't think before. Thanks. L + (R-L)/2 = (2L + R - L) /2 = (L + R)/2
@GreatKrish1
@GreatKrish1 7 ай бұрын
I think 2L and L+R will overflow
@zafirhasananogh2421
@zafirhasananogh2421 24 күн бұрын
@@GreatKrish1 they were probably demonstrating how L + (R-L)/2 and (L + R)/2 is the same but does not overflow
@lovleshbhatt7797
@lovleshbhatt7797 4 жыл бұрын
need second part of this lecture as soon as possible , its a humble request from the fan side
@psibarpsi
@psibarpsi Жыл бұрын
This was great. Helped me learn a lot. I would be looking forward to more such sessions.
@raselhasan2433
@raselhasan2433 3 жыл бұрын
I loved your lecture, as a teacher/trainer of coding, you are best. Thanks man.
@himanshulal4454
@himanshulal4454 Жыл бұрын
Excellent way to cover Binary Search using a systematic way.. Thanks a lot
@matiyasyohannes5502
@matiyasyohannes5502 2 жыл бұрын
This is the best lecture on binary search. Thank you Errichto
@amritbhardwaj99
@amritbhardwaj99 3 жыл бұрын
This is a MUST watch vid on binary search!!
@wizardOfRobots
@wizardOfRobots 3 жыл бұрын
Thanks for the great lecture! Instead of a template, you gave me a proper way to think of the algorithm.
@lpdc9767
@lpdc9767 5 жыл бұрын
Thanks so much for the clear and concise material! I hope you keep making these.
@Rich65501
@Rich65501 5 жыл бұрын
Excellent -- thanks for the correct way to calculate the midpoint in binary search.
@doejohn5637
@doejohn5637 Жыл бұрын
what a brilliant idea of thinking binary search problems as elements of search space being T or F !
@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!
@priyanandshukla8388
@priyanandshukla8388 3 жыл бұрын
One of the best lectures on binary search
@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
@rehanakhter4813
@rehanakhter4813 3 жыл бұрын
Best explaination of binary search in youtube. Thanks a lot!
@literallynoone2093
@literallynoone2093 2 жыл бұрын
Thanks a lot man! I was struggling to get a hold of all the variations. You made it so simple!
@sanddevelopers
@sanddevelopers 5 жыл бұрын
Thanks a lot for the clear explanation
@alexneagu4466
@alexneagu4466 5 жыл бұрын
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.)
@kairatopa9564
@kairatopa9564 4 жыл бұрын
Thanks man u made my interview preparation much easier
@chandup832
@chandup832 Ай бұрын
I have lost from rotated array, till then it was awesome!
@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
@KarthikKarthik-el5hh
@KarthikKarthik-el5hh 2 жыл бұрын
thank you so much errichto, ur explanation was awesome now i understand binary search better
@133839297
@133839297 3 жыл бұрын
Let's keep it binary with 2 words: PURE GOLD!
@saitama-fv6my
@saitama-fv6my 2 жыл бұрын
I watched your video and I got explained my topic what I wanted for. thank you so much sir 🙏
@davidjiang7929
@davidjiang7929 4 жыл бұрын
Thanks for sharing! Your explanations are clear and concise. Also congrats on recent win!
@icenberg5908
@icenberg5908 4 жыл бұрын
Thanks errichto for your efforts and clear presentation.
@blu5037
@blu5037 5 жыл бұрын
Wow, super great video. Thanks for making this errichto.
@mehedihasanjoy8726
@mehedihasanjoy8726 5 жыл бұрын
Pls do a leture on basic greedy and DP
@Errichto
@Errichto 5 жыл бұрын
I'm planning a lecture on dp, actually. It should be out soon.
@abhishekdhok5245
@abhishekdhok5245 3 жыл бұрын
CP God for a reason.. Love your efforts. Love from India
@pradiptarakshit35
@pradiptarakshit35 4 жыл бұрын
Please, do a lecture on Graph Algorithms. Also, thanks for this video.
@geekySRM
@geekySRM 3 жыл бұрын
This is so awesome! Thank you so much @Errichto!
@becomingbesthackerprogramm4644
@becomingbesthackerprogramm4644 2 жыл бұрын
Lovely classic lecture , mesmerized by explanations , thanks alot ❤️
@Sean-jv6bd
@Sean-jv6bd 3 жыл бұрын
Hi Errichto this is an amazing lecture! Thank you so much and please keep up the great work!
@yilinma8367
@yilinma8367 4 жыл бұрын
I wish I watched this video earlier. The True & False pattern is so helpful!!!
@SushantKumar-iy4is
@SushantKumar-iy4is 4 жыл бұрын
Well explained, Errichto. Clean and Simple.
@cccctainvista
@cccctainvista 4 жыл бұрын
Great video, love it, you are very good at making complex problems seem very simple!
@thuanvo9283
@thuanvo9283 3 жыл бұрын
Your ways so easy to understand. thank you Eric so much!!!
@omti
@omti 5 жыл бұрын
Very cool content! Thank you! Waiting for more videos...
@abhishekgaurgaur
@abhishekgaurgaur 3 жыл бұрын
Thank you very much Errichto, really appreciate it. ❤️
@SuperArjun11
@SuperArjun11 5 жыл бұрын
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 жыл бұрын
yes, i also say so.
@Errichto
@Errichto 5 жыл бұрын
Yes, I plan both sqrt deco and seg trees :) More video ideas here: github.com/Errichto/youtube/wiki/Video-ideas
@kushagrasahni9708
@kushagrasahni9708 4 жыл бұрын
@@Errichto Please make a video on segment and fenwick trees.
@koocheukkeijacky9704
@koocheukkeijacky9704 5 жыл бұрын
Really appreciate of your videos !! very clear explanation
@Errichto
@Errichto 5 жыл бұрын
Thank you!
@ivanleon6164
@ivanleon6164 3 жыл бұрын
greetings from Mexico! those videos are really cool, thanks for all the effort put on those.
@adityakajale4403
@adityakajale4403 3 жыл бұрын
Best lecture on binary search
@darshpatel8385
@darshpatel8385 4 жыл бұрын
This video was really helpful to me. Thanks for making such good content.
@rohanvtk
@rohanvtk 4 жыл бұрын
2:46 the correct formula and the reason why :)
@areebwadood6273
@areebwadood6273 5 жыл бұрын
Great learned some new applications of binary search
@barakode414
@barakode414 4 жыл бұрын
Thank you very much. It's easy after watching your video.
@abdurrahaman388
@abdurrahaman388 4 жыл бұрын
Most informative binary search tutorial ❤️
@laskdkmgful
@laskdkmgful 5 жыл бұрын
Best lecturer I have seen. Thanks!
@__-to3hq
@__-to3hq 5 жыл бұрын
"The study from 1988 showed that only one in four textbooks has a correct implementation..."
@beanlighter9491
@beanlighter9491 4 жыл бұрын
Thank you so much sir. Countering problems with that T...F was quite interesting I will try sometimes
@primerevan8403
@primerevan8403 3 жыл бұрын
Just clicked before thinking let's watch something might come up .
@murahat98
@murahat98 3 жыл бұрын
Everyone else: Use (l+r)/2 to find the middle element. No problem at all. Errichto: Never use this formula xD
@avisekshaw4748
@avisekshaw4748 4 жыл бұрын
Great work bro. Thank you for uploading such a great content.
@bharat_india12
@bharat_india12 Жыл бұрын
In this video first find the range which satisfy which is true and not satisfied which is false and applied binary search in the range to find last true or first true of first false or last false . Other case in which only one point is answer use mid and check goes to left or right or current mid is answr to return.
@teezp12490a
@teezp12490a 5 жыл бұрын
Thanks for the video! Came here from Joma's vid
@rajgarg8883
@rajgarg8883 5 жыл бұрын
Thanks, Please make videos on educational round problems.
@omanshsharma6796
@omanshsharma6796 2 жыл бұрын
thanks a lot enrichto!! beautifully explained!!
@aabhassaxena2490
@aabhassaxena2490 5 жыл бұрын
The overflow you talked about in the java library Will have no affect in competitive coding bcoz there the constraints are small so we will not have array of such big size and therefore overflow will never occur
@Errichto
@Errichto 5 жыл бұрын
1) We will eventually work with arrays of size 2e9. It might already happen in unusual contests like Distributed Code Jam. 2) The array is not always given explicitly - examples are computing floor(sqrt(x)) or interactive problems. It's better to use binary search that will always work.
@yamisekai
@yamisekai 4 жыл бұрын
Thank you for sharing your knowledge, great video!
@ajr1791ze
@ajr1791ze 5 жыл бұрын
amazing lecture. Thanks !
Dynamic Programming lecture #1 - Fibonacci, iteration vs recursion
19:47
Errichto Algorithms
Рет қаралды 316 М.
Binary Exponentiation
15:13
Errichto Algorithms
Рет қаралды 102 М.
Players push long pins through a cardboard box attempting to pop the balloon!
00:31
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 21 МЛН
Binary Search - A Different Perspective | Python Algorithms
8:56
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 696 М.
Google Coding Interview With A Competitive Programmer
54:17
Clément Mihailescu
Рет қаралды 2,5 МЛН
Prefix Sums - Problems, Code in C++ & Python
20:51
Errichto Algorithms
Рет қаралды 56 М.
Binary Search Algorithm - Computerphile
18:34
Computerphile
Рет қаралды 164 М.
C++ Bitsets in Competitive Programming
15:35
Errichto Algorithms
Рет қаралды 121 М.
NVIDIA’s New AI: Stunning Voice Generator!
6:21
Two Minute Papers
Рет қаралды 86 М.
How Binary Search Makes Computers Much, Much Faster
6:51
Tom Scott
Рет қаралды 1,4 МЛН
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 404 М.