Binary Search with C++ STL | 4 Problems followed up | Lower Bound and Upper Bound explained

Sdílet
Vložit
  • čas přidán 7. 05. 2020
  • Check our Website:
    In case you are thinking to buy courses, please check below:
    Link to get 20% additional Discount at Coding Ninjas: bit.ly/3wE5aHx
    Code "takeuforward" for 15% off at GFG: practice.geeksforgeeks.org/co...
    Code "takeuforward" for 20% off on sys-design: get.interviewready.io?_aff=takeuforward
    Crypto, I use the Wazirx app: wazirx.com/invite/xexnpc4u
    Take 750 rs free Amazon Stock from me: indmoney.onelink.me/RmHC/idje...
    Earn 100 rs by making a Grow Account for investing: app.groww.in/v3cO/8hu879t0
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    ---------------------------------------------------------------------------------------------------------------------------------------------------- Binary Search is one of the most widely used algorithms in programming. Its good to know some shortcuts which saves a lot of time in Contests. However, in interviews it is highly advised to code Binary Search.
    At 0:50 I said a+n points to the last element, it will be it points to after the last element, also the function takes range [Start, end). which means takes the starting element, and excludes the last element.
    Please subscribe to the channel if you have not done it. Do leave a like and a comment, as CZcams recommends videos with more likes and comments.
    Join out telegram channel to interact directly with me. Thanks!
    Link: t.me/Competitive_Programming_tuf

Komentáře • 133

  • @chishikiendeavourer8663
    @chishikiendeavourer8663 Před 8 měsíci +8

    Whether you are doing an interview or working on your project, always stick to the STL. The algorithms in STL are written in the most optimized way. They can outperform the same algorithms written in C. That’s the promise of STL: either it runs faster than C code or at least not slower than C.

  • @utsavsingh899
    @utsavsingh899 Před 4 lety +83

    the end pointer points to the element after the last element, like a.end() will point to the element after the last element.. also a + n will point to the nth element (which doesn't actually exist, it's a theoretical end position).. the pointers point like [start, end)..

    • @takeUforward
      @takeUforward  Před 4 lety +34

      Yeah I missed it I know this one that a.end() points to after the end element. Did not check out for a+n, thanks for the update, will pin this comment.

    • @manishshee6990
      @manishshee6990 Před 3 lety +3

      Ok, a+n also points to the element after last element, which does not occur in the array....

    • @divyanshushukla795
      @divyanshushukla795 Před 3 lety

      @@takeUforward op 🎉 to 🎉🎉 to

    • @II_xD_II
      @II_xD_II Před 3 lety

      ​@@manishshee6990 it exists but its a null operator
      you got the point rite?

  • @takeUforward
    @takeUforward  Před 4 lety +28

    Learn the entire C++ STL here, czcams.com/video/zBhVZzi5RdU/video.html.
    Note: the end pointer points to the element after the last element, like a.end() will point to the element after the last element.. also a + n will point to the nth element (which doesn't actually exist, it's a theoretical end position).. the pointers point like [start, end)..

  • @Nishi-Tiwari
    @Nishi-Tiwari Před rokem +9

    The way you simplified the explanantion is amazing!

  • @rohitsai806
    @rohitsai806 Před 4 lety

    Wonderful demonstration of stl binary search

  • @ShivamSingh-vu8vq
    @ShivamSingh-vu8vq Před 4 lety +7

    Yesterday I was getting TLE on a problem because I was doing linear search , and decided to learn binary search and here it is !

  • @agx111
    @agx111 Před rokem +3

    9:20 in this we could write index instead of -1 in the else statement in cout

  • @deepakkoushal56
    @deepakkoushal56 Před 4 lety +35

    what a coincidence sir I was learning this STL a few minutes ago and you make a video on this thanks a lot sir!!

  • @chinturadix9360
    @chinturadix9360 Před 3 lety +1

    Amazing the problem set as well as explanation

  • @lyricalfade1966
    @lyricalfade1966 Před 2 lety +1

    How someone can explain with this ease....thanks bhaiya

  • @ravisachdeva9401
    @ravisachdeva9401 Před 4 lety

    Very good video.. I like ur way of teaching.. Pls make more such quick and topic specific video..
    liked and subscribed ur channel

  • @mdkashifraza2603
    @mdkashifraza2603 Před 2 lety +1

    Extremely helpful thanks a lot🙂

  • @Mrswapsam13
    @Mrswapsam13 Před 3 lety +3

    I was confusing lower bound with floor value. thank you so much!

  • @manojgollapelli9856
    @manojgollapelli9856 Před 2 lety +1

    Really Helpful , Thank you !

  • @manojgollapelli3026
    @manojgollapelli3026 Před 2 lety

    very well explained thank you : )

  • @vakhariyajay2224
    @vakhariyajay2224 Před rokem +1

    Thank you very much. You are a genius. 👍👍👌👌🙏🙏🔝🔝

  • @rahulyadav8159
    @rahulyadav8159 Před 4 lety +1

    Banda achha explain karta hai!

  • @evergreen7781
    @evergreen7781 Před 4 lety +18

    Undoubtedly the best video tutorial on the topic ❤️❤️❤️ Good job @Striver

  • @TheGeekyExplorer
    @TheGeekyExplorer Před 3 lety

    Extremely helpful thanks a lot

  • @ALLMOONTASIR__
    @ALLMOONTASIR__ Před 4 lety

    Thanks a lot brother i will be always greatfull for your video

  • @HarshitSingh-it7kp
    @HarshitSingh-it7kp Před 4 lety +5

    No kidding , You really explain very well . Thank you for all these videos . Really very helpful!!!!

  • @nagavijaykumarprathi8531
    @nagavijaykumarprathi8531 Před 4 lety +5

    Awesome explanation bro...
    Please try to make more videos on STL..
    Thank you

  • @AbhishekKumar-im2xd
    @AbhishekKumar-im2xd Před 4 lety +7

    You are a gem 💎💎💎

  • @pavanaditya2309
    @pavanaditya2309 Před 3 lety +1

    The video is really good. I think you should make videos on the algorithms and techniques also. It helps a lot of students because your teaching is good.

  • @freeandreliablejeeprep820
    @freeandreliablejeeprep820 Před 7 měsíci +2

    thank you srila prabhupad, krishna and sir

  • @akanshasingh6521
    @akanshasingh6521 Před 3 lety

    Well explained Thankyou.

  • @sudhanshujha9424
    @sudhanshujha9424 Před 2 lety +1

    i have a question can we use stl in job interviews ??

  • @shwetasaha6687
    @shwetasaha6687 Před rokem

    Explained really well!

  • @smoothierudiee
    @smoothierudiee Před 12 dny

    thank u so much for this wonderful video

  • @sagnikroy6555
    @sagnikroy6555 Před 4 lety

    Thanks vaiya.. Implementing on BS was too complex.. These will definitely help..💫💫💫😇

  • @arnabchakraborty246
    @arnabchakraborty246 Před 4 lety

    Thank you very much

  • @srinivasvoore
    @srinivasvoore Před 4 lety +1

    Bro do more STL videos which helps in CP.

  • @sriramadithya4799
    @sriramadithya4799 Před 2 lety

    Great quality content 👌

  • @solankibhavyaghanshyambhai9886

    Wow bhai, learned something new

  • @Abhishek-fo3fc
    @Abhishek-fo3fc Před 3 lety +1

    Very nice bhaiya 👍

  • @yash.dwivedi
    @yash.dwivedi Před 4 lety

    Thanks a lot sir

  • @ankitchaudhary3708
    @ankitchaudhary3708 Před 4 lety

    Thanks bhaiya for this video

  • @MangoLassiYT
    @MangoLassiYT Před rokem

    if the iterator goes out of range does it returns garbage val

  • @apurvadaga6531
    @apurvadaga6531 Před 4 lety +2

    Amazing videos. I'm actually the first one to watch all the videos you make. Thank You!

  • @sameekshasingh2411
    @sameekshasingh2411 Před 3 lety

    Well explained!

  • @PawanKumar-tu6ti
    @PawanKumar-tu6ti Před 2 lety

    Thanks Raj bhaiya.

  • @sudheersingh2515
    @sudheersingh2515 Před 4 lety

    great video sir ...

  • @anything_jara_hatke5237

    Really you are a hero. Thanks for such a nice explanation. We even don't require the long geeks code after seeing your video.

  • @Mehraj_IITKGP
    @Mehraj_IITKGP Před 2 lety

    thank you, sir

  • @zacian4941
    @zacian4941 Před 4 lety

    Thank You

  • @Firstusee256
    @Firstusee256 Před 4 lety +1

    Awesome.

  • @Bharat_Rider
    @Bharat_Rider Před rokem +1

    Maja agya bhai bahut kam ha chiz hai

  • @DharanAditya
    @DharanAditya Před 4 lety +1

    This was great video. Can you make videos about Binary Search algorithm on monotonic function and modified version of it.

  • @ShivamSingh-vu8vq
    @ShivamSingh-vu8vq Před 4 lety +2

    Should the array be previously sorted to perform lower and upper bound

  • @ShivamGupta-po7rg
    @ShivamGupta-po7rg Před 2 lety

    tq

  • @spacehole9396
    @spacehole9396 Před rokem

    thank you

  • @tanishqgupta1071
    @tanishqgupta1071 Před 4 lety

    hi bhaiya can you make some videos on c++ stl it is very confusing to learn from online articles plz make these videos its a humble request

  • @changed217
    @changed217 Před rokem

    Thank you.

  • @SushmaRaiei
    @SushmaRaiei Před rokem +1

    Lower_bound(start_ptr, end_ptr, num) STL in c++ has one weird return value
    If it doesn't find the value 'num' it returns the immediate higher number value of num. What is the use of this new higher value ?

    • @guptashashwat
      @guptashashwat Před rokem

      If u want to just find the number greater irrespective whether that element is present or not

  • @karansinghrawat2000
    @karansinghrawat2000 Před 4 lety

    bro no one explain like you

  • @anjalisah1452
    @anjalisah1452 Před 3 lety

    Thanks for taking us forword ❤️😁

  • @saketjaiswal9381
    @saketjaiswal9381 Před rokem

    again this video also completed god or wott brother😎😜😎

  • @VaibhavTalks
    @VaibhavTalks Před rokem

    Amazing Video 😀😀♥♥

  • @vishadjain2696
    @vishadjain2696 Před 4 lety +1

    is it possible to find upper_bound and lower_bound in an unsorted array?

  • @niteshsawlapurkar4159
    @niteshsawlapurkar4159 Před měsícem

    understood! well

  • @prakhar266
    @prakhar266 Před 2 lety

    just wow.....

  • @Firstusee256
    @Firstusee256 Před 4 lety +3

    Please upload more dynamic programming videos

    • @jashanbansal2613
      @jashanbansal2613 Před 4 lety +1

      +1

    • @takeUforward
      @takeUforward  Před 4 lety +13

      Sorry bro, I am really buzy with my ongoing schedule, but then I have plans on launching a series on Atcoder DP problems soon.

    • @ShivamSingh-vu8vq
      @ShivamSingh-vu8vq Před 4 lety +1

      +1

    • @pranavn2554
      @pranavn2554 Před 4 lety

      @@takeUforward that's great even coding blocks have made it a paid course of problems in atcoder pls bro start the series as soon as possible

  • @fahimtoufiqulislam9264
    @fahimtoufiqulislam9264 Před 7 měsíci

    the upper bound condition check should be like this. you missed the -1 from index.
    if( ind >= 0 && [ ind - 1 ] ==x)cout

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

      i did not get the boundary conditions are not they written opposite. I am confused

    • @user-bg4gl4wt7v
      @user-bg4gl4wt7v Před měsícem

      he already did i - - before writing a[ind]==X; Check.

  • @shishankrawat2105
    @shishankrawat2105 Před 3 lety +2

    You literally speak like the girl in the American Pie who kept saying:
    "Aah,.... that one time, ..at Band Camp"😂😅.
    But you really are a solid teacher.. loved the video.

  • @hemantvardani1436
    @hemantvardani1436 Před 2 lety

    THANKS ,,GREAT VIEDO

  • @AnshuKumar-zn1qb
    @AnshuKumar-zn1qb Před 3 lety

    👍👍👍keep going striver bhai...

  • @ankitkumarsingh9815
    @ankitkumarsingh9815 Před 4 lety

    Nice Bhaiya

  • @ankanachakraborty3193
    @ankanachakraborty3193 Před 2 měsíci +1

    does upper_bound() and lower_bound() work only for sorted vectors / arrays?

  • @kushagrasaxena8831
    @kushagrasaxena8831 Před 4 lety

    Best🔥

  • @salonikarki1462
    @salonikarki1462 Před 3 lety +1

    Why do we do (-a) for taking out the index of lower bound or upper bound....plzz answer i am stuck here
    In the line ...int ind= upper _bound(a,a+n,X) -a;

    • @takeUforward
      @takeUforward  Před 3 lety +1

      Because it returns an iterator, so to get the index we subtract starting indez

    • @salonikarki1462
      @salonikarki1462 Před 3 lety

      @@takeUforward one more thing I still have a doubt sir suppose the iterator is 4 so when we subtract the iterator from a(a is the first address of the array right?) So suppose (4- address of a) so as a is the address of first element right so again if we substract 4 then something else is coming
      Sorry I am thinking this way so I am stuck...I think I am thinking wrongly I know
      So sir plzz elaborate it a little more nd what wrong I am thinking...plzz sir

    • @takeUforward
      @takeUforward  Před 3 lety

      @@salonikarki1462if the address of first element is 1612 then second element will be 1613 and so on, its consecutive. Hence you get 0-based indexing on subtraction.

    • @salonikarki1462
      @salonikarki1462 Před 3 lety

      @@takeUforward oo yes yes bro thank you very much now I understood...I was subtracting index value with address instead of subtracting address value with first address to get the index...thank you very much😇

    • @garvbhatt5133
      @garvbhatt5133 Před 2 lety +1

      @@takeUforward sir How 1612's next address is 1613
      why it is not 1616

  • @funenjoynilaypatel4553
    @funenjoynilaypatel4553 Před 2 lety +1

    3 march 2022 10-30am

  • @himanshutripathi5216
    @himanshutripathi5216 Před 3 lety

    For question 2, tell me a case where ind will be less than 0 . There will be no case when ind will become less than 0. Lets say, we passed X=1 in this questions. upper_bound will return iterator pointing to element "4" which is located at index 1. We will do ind-- , so finally our ind will become ind = 0. I can't think of a case where ind will be less than 0. So I think even if we remove ind>=0 from the if condition program will do fine. Please do reply if anyone understood what I'm trying to say.

  • @parthosarkar5270
    @parthosarkar5270 Před 4 lety

    Very helpful as always!

  • @prithvijitbasak
    @prithvijitbasak Před 3 lety

    Extraordinary explanation 🔥🔥

  • @adishijha8365
    @adishijha8365 Před 3 lety

    Understood

  • @JohnWickea
    @JohnWickea Před 10 měsíci

    ek hi to dil hai kitni baar jeetoge Striver 😇❤

  • @1moreredcoder
    @1moreredcoder Před rokem

    ur lower bound explation is wrong it gives just smaller index but u told it give us >= index please checj it again

  • @mongalmoykarmakar283
    @mongalmoykarmakar283 Před 3 lety

    ❤️❤️

  • @lomeshdaheria9960
    @lomeshdaheria9960 Před 3 lety

    Thanks striver

  • @aniruddhmishra6182
    @aniruddhmishra6182 Před 2 lety +1

    Striver OP

  • @crazyboy-gw7rk
    @crazyboy-gw7rk Před 3 lety +1

    Pls make video on complete stl in detail like this one,
    Amazing video 🙂

    • @takeUforward
      @takeUforward  Před 3 lety +2

      Made, check in channel

    • @crazyboy-gw7rk
      @crazyboy-gw7rk Před 3 lety

      @@takeUforward as i have started learning programming, problem that i face is when i start one topic in data structures, in question(medium) they use some algo which i don,t know, wheni start algo problem solving(medium), they use data structure with it so all this make path confusing, can u make video on this?

  • @gourabsaha1370
    @gourabsaha1370 Před 4 lety

    💙💙

  • @Nishant_0073
    @Nishant_0073 Před 4 lety +1

    while(1)
    {
    cout

  • @valobhediya
    @valobhediya Před rokem +1

    King 👑👈

  • @ShreyaSingh-vr9qi
    @ShreyaSingh-vr9qi Před 4 lety

    Bro, plzz make videos on segment tree, Fenwick tree, trie.

  • @apurvadaga6531
    @apurvadaga6531 Před 4 lety

    can you please make some videos of python?and also div 4 solutions?

  • @ehtesamzunnuryn282
    @ehtesamzunnuryn282 Před 10 měsíci

    gold

  • @naymulislam7445
    @naymulislam7445 Před 3 lety

    halpful

  • @farzicoder2699
    @farzicoder2699 Před 3 lety

    Thank you bhaiya __/\__

  • @abhishekjha1064
    @abhishekjha1064 Před 4 lety

    Nice explanation striver

  • @abhijeetkumar2204
    @abhijeetkumar2204 Před 4 lety

    Great video...fuk scalar fraud