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

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ต.ค. 2024
  • 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.geeks...
    Code "takeuforward" for 20% off on sys-design: get.interviewr...?_aff=takeuforward
    Crypto, I use the Wazirx app: wazirx.com/inv...
    Take 750 rs free Amazon Stock from me: indmoney.oneli...
    Earn 100 rs by making a Grow Account for investing: app.groww.in/v...
    Linkedin/Instagram/Telegram: linktr.ee/take...
    ---------------------------------------------------------------------------------------------------------------------------------------------------- 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 TH-cam recommends videos with more likes and comments.
    Join out telegram channel to interact directly with me. Thanks!
    Link: t.me/Competiti...

ความคิดเห็น • 135

  • @utsavsingh899
    @utsavsingh899 4 ปีที่แล้ว +87

    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  4 ปีที่แล้ว +37

      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 4 ปีที่แล้ว +3

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

    • @divyanshushukla795
      @divyanshushukla795 4 ปีที่แล้ว

      @@takeUforward op 🎉 to 🎉🎉 to

    • @II_xD_II
      @II_xD_II 4 ปีที่แล้ว

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

  • @Sobioytccc
    @Sobioytccc 10 หลายเดือนก่อน +13

    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.

  • @Nishi-Tiwari
    @Nishi-Tiwari ปีที่แล้ว +11

    The way you simplified the explanantion is amazing!

  • @takeUforward
    @takeUforward  4 ปีที่แล้ว +28

    Learn the entire C++ STL here, th-cam.com/video/zBhVZzi5RdU/w-d-xo.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)..

  • @deepakkoushal56
    @deepakkoushal56 4 ปีที่แล้ว +36

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

  • @agx111
    @agx111 ปีที่แล้ว +3

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

  • @ShivamSingh-vu8vq
    @ShivamSingh-vu8vq 4 ปีที่แล้ว +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 !

  • @evergreen7781
    @evergreen7781 4 ปีที่แล้ว +18

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

  • @lyricalfade1966
    @lyricalfade1966 3 ปีที่แล้ว +1

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

  • @fahimtoufiqulislam9264
    @fahimtoufiqulislam9264 9 หลายเดือนก่อน

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

    • @piyushgoyal9284
      @piyushgoyal9284 4 หลายเดือนก่อน

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

    • @MAYANVAJANI
      @MAYANVAJANI 4 หลายเดือนก่อน

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

  • @HarshitSingh-it7kp
    @HarshitSingh-it7kp 4 ปีที่แล้ว +5

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

  • @Mrswapsam13
    @Mrswapsam13 4 ปีที่แล้ว +3

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

  • @nagavijaykumarprathi8531
    @nagavijaykumarprathi8531 4 ปีที่แล้ว +5

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

    • @takeUforward
      @takeUforward  4 ปีที่แล้ว +5

      A complete tutorial on how to learn stl in 30 minutes coming your way.

    • @nagavijaykumarprathi8531
      @nagavijaykumarprathi8531 4 ปีที่แล้ว

      @@takeUforward Tq bro...
      That's awesome.. 😍

  • @shishankrawat2105
    @shishankrawat2105 3 ปีที่แล้ว +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.

  • @ShivamSingh-vu8vq
    @ShivamSingh-vu8vq 4 ปีที่แล้ว +3

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

    • @takeUforward
      @takeUforward  4 ปีที่แล้ว +1

      Yes, every question states sorted array.

  • @freeandreliablejeeprep820
    @freeandreliablejeeprep820 9 หลายเดือนก่อน +3

    thank you srila prabhupad, krishna and sir

  • @AbhishekKumar-im2xd
    @AbhishekKumar-im2xd 4 ปีที่แล้ว +7

    You are a gem 💎💎💎

  • @rohitsai806
    @rohitsai806 4 ปีที่แล้ว

    Wonderful demonstration of stl binary search

  • @SushmaRaiei
    @SushmaRaiei ปีที่แล้ว +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 ปีที่แล้ว

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

  • @pavanaditya2309
    @pavanaditya2309 4 ปีที่แล้ว +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.

  • @sudhanshujha9424
    @sudhanshujha9424 3 ปีที่แล้ว +1

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

  • @DharanAditya
    @DharanAditya 4 ปีที่แล้ว +1

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

  • @rahulyadav8159
    @rahulyadav8159 4 ปีที่แล้ว +1

    Banda achha explain karta hai!

  • @Firstusee256
    @Firstusee256 4 ปีที่แล้ว +3

    Please upload more dynamic programming videos

    • @jashanbansal2613
      @jashanbansal2613 4 ปีที่แล้ว +1

      +1

    • @takeUforward
      @takeUforward  4 ปีที่แล้ว +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 4 ปีที่แล้ว +1

      +1

    • @pranavn2554
      @pranavn2554 4 ปีที่แล้ว

      @@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

  • @chinturadix9360
    @chinturadix9360 3 ปีที่แล้ว +1

    Amazing the problem set as well as explanation

  • @apurvadaga6531
    @apurvadaga6531 4 ปีที่แล้ว +2

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

  • @himanshutripathi5216
    @himanshutripathi5216 3 ปีที่แล้ว

    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.

  • @crazyboy-gw7rk
    @crazyboy-gw7rk 4 ปีที่แล้ว +1

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

    • @takeUforward
      @takeUforward  4 ปีที่แล้ว +2

      Made, check in channel

    • @crazyboy-gw7rk
      @crazyboy-gw7rk 4 ปีที่แล้ว

      @@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?

  • @sagnikroy6555
    @sagnikroy6555 4 ปีที่แล้ว

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

  • @vakhariyajay2224
    @vakhariyajay2224 ปีที่แล้ว +1

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

  • @manojgollapelli9856
    @manojgollapelli9856 2 ปีที่แล้ว +1

    Really Helpful , Thank you !

  • @Bharat_Rider
    @Bharat_Rider ปีที่แล้ว +1

    Maja agya bhai bahut kam ha chiz hai

  • @ankanachakraborty3193
    @ankanachakraborty3193 4 หลายเดือนก่อน +1

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

  • @vishadjain2696
    @vishadjain2696 4 ปีที่แล้ว +1

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

    • @takeUforward
      @takeUforward  4 ปีที่แล้ว +3

      No works only in sorted array

  • @mdkashifraza2603
    @mdkashifraza2603 2 ปีที่แล้ว +1

    Extremely helpful thanks a lot🙂

  • @tanishqgupta1071
    @tanishqgupta1071 4 ปีที่แล้ว

    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

  • @MangoLassiYT
    @MangoLassiYT ปีที่แล้ว

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

  • @anything_jara_hatke5237
    @anything_jara_hatke5237 4 ปีที่แล้ว

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

  • @ravisachdeva9401
    @ravisachdeva9401 4 ปีที่แล้ว

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

  • @salonikarki1462
    @salonikarki1462 3 ปีที่แล้ว +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  3 ปีที่แล้ว +1

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

    • @salonikarki1462
      @salonikarki1462 3 ปีที่แล้ว

      @@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  3 ปีที่แล้ว

      @@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 3 ปีที่แล้ว

      @@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 2 ปีที่แล้ว +1

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

  • @srinivasvoore
    @srinivasvoore 4 ปีที่แล้ว +1

    Bro do more STL videos which helps in CP.

  • @1moreredcoder
    @1moreredcoder ปีที่แล้ว

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

  • @rumiNITPatna
    @rumiNITPatna 2 หลายเดือนก่อน

    thank u so much for this wonderful video

  • @ALLMOONTASIR__
    @ALLMOONTASIR__ 4 ปีที่แล้ว

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

  • @Abhishek-fo3fc
    @Abhishek-fo3fc 3 ปีที่แล้ว +1

    Very nice bhaiya 👍

  • @TheGeekyExplorer
    @TheGeekyExplorer 3 ปีที่แล้ว

    Extremely helpful thanks a lot

  • @shwetasaha6687
    @shwetasaha6687 ปีที่แล้ว

    Explained really well!

  • @arnabchakraborty246
    @arnabchakraborty246 4 ปีที่แล้ว

    Thank you very much

  • @akanshasingh6521
    @akanshasingh6521 3 ปีที่แล้ว

    Well explained Thankyou.

  • @manojgollapelli3026
    @manojgollapelli3026 3 ปีที่แล้ว

    very well explained thank you : )

  • @PawanKumar-tu6ti
    @PawanKumar-tu6ti 3 ปีที่แล้ว

    Thanks Raj bhaiya.

  • @Firstusee256
    @Firstusee256 4 ปีที่แล้ว +1

    Awesome.

  • @spacehole9396
    @spacehole9396 ปีที่แล้ว

    thank you

  • @solankibhavyaghanshyambhai9886
    @solankibhavyaghanshyambhai9886 2 ปีที่แล้ว

    Wow bhai, learned something new

  • @changed217
    @changed217 ปีที่แล้ว

    Thank you.

  • @Mehraj_IITKGP
    @Mehraj_IITKGP 2 ปีที่แล้ว

    thank you, sir

  • @apurvadaga6531
    @apurvadaga6531 4 ปีที่แล้ว

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

  • @sriramadithya4799
    @sriramadithya4799 3 ปีที่แล้ว

    Great quality content 👌

  • @sameekshasingh2411
    @sameekshasingh2411 3 ปีที่แล้ว

    Well explained!

  • @yash.dwivedi
    @yash.dwivedi 4 ปีที่แล้ว

    Thanks a lot sir

  • @VaibhavTalks
    @VaibhavTalks ปีที่แล้ว

    Amazing Video 😀😀♥♥

  • @JohnWickea
    @JohnWickea ปีที่แล้ว

    ek hi to dil hai kitni baar jeetoge Striver 😇❤

  • @saketjaiswalSJ
    @saketjaiswalSJ ปีที่แล้ว

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

  • @sudheersingh2515
    @sudheersingh2515 4 ปีที่แล้ว

    great video sir ...

  • @ShreyaSingh-vr9qi
    @ShreyaSingh-vr9qi 4 ปีที่แล้ว

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

  • @ankitchaudhary3708
    @ankitchaudhary3708 4 ปีที่แล้ว

    Thanks bhaiya for this video

  • @niteshsawlapurkar4159
    @niteshsawlapurkar4159 3 หลายเดือนก่อน

    understood! well

  • @ShivamGupta-po7rg
    @ShivamGupta-po7rg 2 ปีที่แล้ว

    tq

  • @karansinghrawat2000
    @karansinghrawat2000 4 ปีที่แล้ว

    bro no one explain like you

  • @funenjoynilaypatel4553
    @funenjoynilaypatel4553 2 ปีที่แล้ว +1

    3 march 2022 10-30am

  • @anjalisah1452
    @anjalisah1452 3 ปีที่แล้ว

    Thanks for taking us forword ❤️😁

  • @parthosarkar5270
    @parthosarkar5270 4 ปีที่แล้ว

    Very helpful as always!

  • @AnshuKumar-zn1qb
    @AnshuKumar-zn1qb 3 ปีที่แล้ว

    👍👍👍keep going striver bhai...

  • @aasthasingh752
    @aasthasingh752 หลายเดือนก่อน

    understood

  • @hemantvardani1436
    @hemantvardani1436 2 ปีที่แล้ว

    THANKS ,,GREAT VIEDO

  • @prakhar266
    @prakhar266 2 ปีที่แล้ว

    just wow.....

  • @adishijha8365
    @adishijha8365 3 ปีที่แล้ว

    Understood

  • @ankitkumarsingh9815
    @ankitkumarsingh9815 4 ปีที่แล้ว

    Nice Bhaiya

  • @Nishant_0073
    @Nishant_0073 4 ปีที่แล้ว +1

    while(1)
    {
    cout

  • @prithvijitbasak
    @prithvijitbasak 3 ปีที่แล้ว

    Extraordinary explanation 🔥🔥

  • @lomeshdaheria9960
    @lomeshdaheria9960 4 ปีที่แล้ว

    Thanks striver

  • @kushagrasaxena8831
    @kushagrasaxena8831 4 ปีที่แล้ว

    Best🔥

  • @naveenbisht4627
    @naveenbisht4627 หลายเดือนก่อน

  • @aniruddhmishra6182
    @aniruddhmishra6182 2 ปีที่แล้ว +1

    Striver OP

  • @valobhediya
    @valobhediya 2 ปีที่แล้ว +1

    King 👑👈

  • @abhishekjha1064
    @abhishekjha1064 4 ปีที่แล้ว

    Nice explanation striver

  • @mongalmoykarmakar283
    @mongalmoykarmakar283 3 ปีที่แล้ว

    ❤️❤️

  • @gourabsaha1370
    @gourabsaha1370 4 ปีที่แล้ว

    💙💙

  • @ehtesamzunnuryn282
    @ehtesamzunnuryn282 ปีที่แล้ว

    gold

  • @farzicoder2699
    @farzicoder2699 3 ปีที่แล้ว

    Thank you bhaiya __/\__

  • @naymulislam7445
    @naymulislam7445 4 ปีที่แล้ว

    halpful

  • @abhijeetkumar2204
    @abhijeetkumar2204 4 ปีที่แล้ว

    Great video...fuk scalar fraud

  • @zacian4941
    @zacian4941 4 ปีที่แล้ว

    Thank You