BS-2. Implement Lower Bound and Upper Bound | Search Insert Position | Floor and Ceil

Поделиться
HTML-код
  • Опубликовано: 26 окт 2024

Комментарии • 439

  • @takeUforward
    @takeUforward  Год назад +112

    Please comment understood and give us a like if you got everything :)

    • @RachitAgnihotriBEE
      @RachitAgnihotriBEE Год назад +4

      Sir, how lower and upper bound different just by removing an equal to sign. Why lower bound is not the largest value just smaller or equal to the given value?

    • @sudhanshushekhar4222
      @sudhanshushekhar4222 Год назад

      understood

    • @rjedits6717
      @rjedits6717 Год назад

      @@RachitAgnihotriBEE bro why your name is rachit not prachi even if just adding P and removing T from your name. hope so you got the answer there is a difference of equal to sign thats why there name is different as lower and upper bound.

    • @RachitAgnihotriBEE
      @RachitAgnihotriBEE Год назад +1

      @@rjedits6717 mujhe Gyan Dene se accha jakar lower bound or upper bound ka diff dekhle , kabhi set jesa kuchh padha hoga 10th or 12th me to smjh aa jaega ,

    • @jeet-smokey
      @jeet-smokey Год назад

      Understood

  • @saketjaiswalSJ
    @saketjaiswalSJ Год назад +229

    27:00 a litlle error please correct if(a[mid]

  • @shashikumar2382
    @shashikumar2382 Год назад +16

    I was fearing from binary search but once Understood in depth ,I was able to do all question in this lecture my self .
    Thank you so much for this wonderful explanation .Just keep doing it .

  • @shaurya2608
    @shaurya2608 Год назад +72

    Floor and Ceil without using "ans" variable -
    > For floor just do a normal binary search and return end
    > For ceil just do a normal binary search and return start

    • @nagame859
      @nagame859 Год назад +3

      Thanks pal!

    • @nostalgiccringeallhailchel3881
      @nostalgiccringeallhailchel3881 Год назад +1

      doesn't work

    • @zerr0n7eet
      @zerr0n7eet 8 месяцев назад +5

      correct!
      pair getFloorAndCeil(vector &a, int n, int x) {
      int low = 0, high = n-1;
      pair p;
      while(low x){
      high = mid - 1;
      }else{
      low = mid + 1;
      }
      }
      p.first = high >= 0 ? a[high] : -1;
      p.second = low

    • @SWATHYBMIS
      @SWATHYBMIS 7 месяцев назад +3

      yes , because for floor , the end/high pointer needs to go to lower search space (mid-1) and for ceil the low/start pointer needs to go to higher search space (mid + 1)

    • @RaghavSingla-er9ub
      @RaghavSingla-er9ub 4 месяца назад

      thanks daddyyy

  • @shubhamagarwal1434
    @shubhamagarwal1434 2 месяца назад +8

    #Free Education For All.. # Bhishma Pitamah of DSA...You could have earned in lacs by putting it as paid couses on udamey or any other elaerning portals, but you decided to make it free...it requires a greate sacrifice and a feeling of giving back to community, there might be very few peope in world who does this...."विद्या का दान ही सर्वोत्तम दान होता है" Hats Off to you man, Salute from 10+ yrs exp guy from BLR, India.

  • @anuragsingh8910
    @anuragsingh8910 Год назад +36

    0:00 Lower Bound Explanation using examples
    10:53 Lower Bound Implementation
    13:35 Lower Bound Code (in C++)
    13:55 lower_bound() C++ STL method
    15:33 Time Complexity Analysis of Lower Bound
    15:46 Upper Bound Explanation using examples
    17:23 Upper Bound Implementation
    18:56 Upper Bound Code (in C++)
    19:13 Time Complexity of Upper Bound
    19:20 upper_bound() C++ STL method
    19:47 Search Insert Position
    22:53 Floor and Ceil in Sorted Array
    28:44 How is Binary Search making sure that we get the smallest index?
    31:40 Outro

  • @habeeblaimusa4466
    @habeeblaimusa4466 Год назад +11

    I am actually revising my concept and this Binary search series has been helpful.
    Thanks Striver

  • @mianawais99
    @mianawais99 Год назад +7

    Really great series. Made in such a way easy to understand in no time. Great work 👏

  • @sagarhiremath4899
    @sagarhiremath4899 4 месяца назад +71

    i feel the lower bound explanation and the provided GFG questions don't match with each other, question might be modified after some time

    • @tanoy54321
      @tanoy54321 3 месяца назад +1

      Yes, it’s different 🫡

    • @recordings8956
      @recordings8956 3 месяца назад +4

      just add this condition in beginning:
      if(x < v[0]) return -1;

    • @mr.kaushik8518
      @mr.kaushik8518 3 месяца назад

      What is v here​@@recordings8956

    • @aishwarybhat5673
      @aishwarybhat5673 3 месяца назад +1

      ​@@recordings8956 class Solution:
      def findFloor(self,A,N,X):
      l=0
      h =N-1
      ans=N
      if X < A[0]:
      return -1
      while l=X:
      ans = mid
      h = mid-1
      else:
      l =mid+1
      return ans
      I corrected my code but its still not working

    • @pratiksingh4700
      @pratiksingh4700 3 месяца назад

      ​@@aishwarybhat5673
      if(a[mid]

  • @abhishekverma7604
    @abhishekverma7604 Год назад +11

    i am on the verge of completing this a2z sheet ,hopefully i will do it by the end of june by myself on my own pace since the tougher side of the sheet is already been done by me... u r gem of a person Raj bhya..♥

  • @thenriquevicentini
    @thenriquevicentini 5 месяцев назад +2

    I don't usually comment on youtube, but I had to login just to say how incredible was this lesson! Thanks very much for this videos!

  • @DTALKS01
    @DTALKS01 4 месяца назад +3

    Understood everything.... Feels like you will taking me forward from my Village to google or Microsoft or Amazon fore sure ❤❤

  • @sauravchandra10
    @sauravchandra10 Год назад +9

    Completed graph and dp series in the previous 20 days, and I am super excited to complete this BS playlist as well.

    • @kshitijtakarkhede7833
      @kshitijtakarkhede7833 Год назад +1

      how did u practice question , what strategy do you use, cause there are so many videos?

    • @sauravchandra10
      @sauravchandra10 Год назад +2

      @@kshitijtakarkhede7833 I targeted atleast 5 videos daily

    • @kshitijtakarkhede7833
      @kshitijtakarkhede7833 Год назад +2

      @@sauravchandra10 Na bro I wanted to ask how you practice the question cause as days passed by things start to pile up 🥲 , nd then i started to forgot ....how ou beat this?

    • @sauravchandra10
      @sauravchandra10 Год назад +4

      @@kshitijtakarkhede7833 just do these problems 2 3 times because these problems are most likely to be asked

    • @kshitijtakarkhede7833
      @kshitijtakarkhede7833 Год назад

      @@sauravchandra10 thanks bro

  • @senseiAree
    @senseiAree Год назад +14

    Thank you Striver, My Intuition is actually improving. Its not about solving the problems, its about understanding the diverse approach.
    Understood ❤

  • @AyushKumar-kx3oz
    @AyushKumar-kx3oz Год назад +1

    kya padhate hooo bhai tum mera dil happy happy hoo gaya jaise aapne explain kiya hain maza aa gaya
    Understood clearly

  • @HarshKumar-ip5nr
    @HarshKumar-ip5nr Год назад +3

    Completely understood your solution and intuition. One thing I especially like about your channel is the intuition building and clean intuitive code. For eg. in the last part when you told about either low/high to be our answer, it is very intuitive to make ans variable instead of returning either one. Really appreciate the efforts and free content.

  • @cinime
    @cinime Год назад +5

    Understood! Super amazing explanation as always, thank you very very much for your effort!!

  • @vishious14
    @vishious14 9 месяцев назад +2

    UNDERSTOOOOOOOD!!!!, was able to solve both problems as soon as you taught the lowerbound approach.

  • @rahulsidhu5945
    @rahulsidhu5945 Год назад +1

    Sir your lectures are very very helpful to us.. This type of content to provide free is very hard.. We are really thank full to you.. I was watching your lecture from 10 days.. Then today I just realize to do just thankfull to you..😊😊blessings++

  • @rahulkumarsingh5278
    @rahulkumarsingh5278 3 месяца назад

    your understanding about Dsa is excellent

  • @sanjayv-ef6ey
    @sanjayv-ef6ey Месяц назад +1

    thanks for all raj i can see my improvement in problem solving i can't express my feeling

  • @graviton001
    @graviton001 3 месяца назад

    Was able to do every single question after lower bound by my self thanks ❤❤. Literally built my thinking ability.

  • @ananthegde2107
    @ananthegde2107 Год назад +1

    Search insert Position :
    My code :
    int searchInsert(vector& arr, int m)
    {
    // Write your code here.
    int n=arr.size();
    int left=0;
    int right=n-1;
    if(m>arr[n-1]){
    return n;
    }
    if(m

  • @nitesh1543
    @nitesh1543 Год назад +16

    In java there is no direct method to find lower bound or upper bound but indirectly you can find with the help of Arrays.binarySearch(arr,target) method
    public class Solution {
    public static int lowerBound(int []arr, int n, int x) {
    // Write your code here
    int ans = Arrays.binarySearch(arr, x); // built in method for binary search it will return the smallest index where target is present i.e lower bound else it will return a negative value
    if(ans

  • @areebahmed3561
    @areebahmed3561 3 месяца назад +1

    Small Small 9:17 wow, what an enthusiasm for teaching and what a style to deliver I wish I had these skills too love you striver bhaii :)

  • @OscarMartinez-nt6zn
    @OscarMartinez-nt6zn 4 месяца назад +1

    This content is gold.

  • @codiescoderszone1518
    @codiescoderszone1518 Год назад +3

    this man is a gem!

  • @utsavseth6573
    @utsavseth6573 Год назад +1

    Very beautifully explained Raj. And that ans technique was too good. Really helpful. Good work bhai.

  • @harshverm776
    @harshverm776 9 месяцев назад

    Yes, Initially on pen & paper. I tried to solve the problem by myself only. There I was getting help from low & high pointers only but there were some of the end cases which I was not able to pass.

  • @jyothishreddynagaram6789
    @jyothishreddynagaram6789 6 месяцев назад +1

    understood everything. You are doing great work, keep it up!

  • @ReNaq313
    @ReNaq313 4 месяца назад +1

    for floor we can also do upper_bound index -- 1 to get the index of floor

  • @kiranmoura2974
    @kiranmoura2974 Год назад +1

    You explained it very well .... thank you so much sir for your efforts .❤

  • @ashizhamal9944
    @ashizhamal9944 Месяц назад

    Bro, You are awesome. I will try to make video from my side as well. The content will be inspired from you rather than copied. As this makes mine coding habit better. Thanks

  • @AtulKumar-c4x7l
    @AtulKumar-c4x7l Год назад +1

    understood
    Thank You @striver for making things so easy...❤❤

  • @wog4299
    @wog4299 Год назад

    yet another amazing explanation by striver

  • @smitj5404
    @smitj5404 9 месяцев назад

    Much love to your work Raj bhai (Striver)❤🙏

  • @amitkoushik5504
    @amitkoushik5504 Год назад +1

    Wowww .... finally 🎉🎉❤❤thank you striver 🤗🤗

  • @them.pranay7196
    @them.pranay7196 Год назад

    TQSM understood Very Elegant way of code

  • @4353_SUSHANTYADAV
    @4353_SUSHANTYADAV 5 месяцев назад

    you have beautifully explained this topic

  • @praveencv431
    @praveencv431 5 месяцев назад +1

    @21:12 When the array is [1, 2, 4, 7] and your target is 2, you need to return the index of 2 as it is present in the array. Please revisit the question @19:57. The Search Insert Position does not reduce to Lower Bound problem.

  • @harshitjaiswal9439
    @harshitjaiswal9439 9 месяцев назад

    Understood sir! Golden video!

  • @elco7956
    @elco7956 4 месяца назад

    Upper bound - first occurance of smallest number in array such that number > target
    Lower bound - first occurance of smallest number in array such that number >= target - same as ceil
    Floor - first occurance of largest number in array such that number

  • @uday2159
    @uday2159 7 месяцев назад +1

    Instead of using 'ans' variable
    For floor -> return low-1
    For ceil -> return high+1
    🎉🎉🎉

  • @siddesh1210
    @siddesh1210 Год назад

    Amazing and just a cake walk now.Bhaiya I'm bit confused and figuring out the right way we should learn DSA , I mean which roadmap must be followed while learning DSA i.e String --> Array--> .... something like this.If anyone know please do let me know.
    And huge love and appreciation for your dedication and hardwork❤.Thankyou soo much bhaiya

    • @omsatpathy5455
      @omsatpathy5455 Год назад +3

      Try to cover arrays first. After that go for strings. Then you can go for linked lists, stacks and queues and then step onto advance data structures like graphs, trees and tries.
      After completing all these, focus on design and analysis of algorithms like DP, Greedy and try optimizing the all the data structures that you've learnt.

  • @AbhishekGrover-w4f
    @AbhishekGrover-w4f 5 месяцев назад +1

    Really helpful lec

  • @hareshnayak7302
    @hareshnayak7302 7 месяцев назад

    Understood,Thank you striver for this amazing video.

  • @AkshitGupta-mp6he
    @AkshitGupta-mp6he 5 месяцев назад

    awesome lecture... what an explanation 🔥🔥🔥🔥

  • @abdullahanis5761
    @abdullahanis5761 Год назад

    Here it is finally

  • @dgo4490
    @dgo4490 7 месяцев назад

    (low + high) could overflow, you should low + (high - low) so it doesn't break for large data

  • @infernogamer52
    @infernogamer52 Год назад

    Understood Bhaiya!
    Very well explained...

  • @falaknaaz789
    @falaknaaz789 Год назад

    Understood...explanation was really awesome ❤😊

  • @mohammadasif5798
    @mohammadasif5798 3 месяца назад

    crystal clear understood thank you so much

  • @shrirambalaji2915
    @shrirambalaji2915 Год назад

    Understood, Great explanation Brother, Thank you

  • @shivarajbandaru2216
    @shivarajbandaru2216 Год назад

    Great class. i liked it a lot . Thank 😀u for simple explaination

  • @ayushsharma-bi7zj
    @ayushsharma-bi7zj 3 месяца назад

    understood amazing explaination

  • @MAHESHAITBECS
    @MAHESHAITBECS 3 месяца назад

    striver u r a genius ❣

  • @kaushiksen2190
    @kaushiksen2190 Год назад

    Understood Sir. Loved it. Thank you

  • @krishnarao3933
    @krishnarao3933 9 месяцев назад +1

    my approach is like i will be searching for the element -1)if found return the index 2) if not return the next lower bound where it stopped
    But 1 testcase in optimal code test is failed. I just want to know where i am missing?😕 Even the above code giving the same.
    int searchInsert(vector& arr, int m)
    {
    // Write your code here.
    int low = 0;
    int high = arr.size()-1;

    while(high>=low){
    int mid = low+(high-low)/2;
    if(arr[mid]==m){
    return mid;
    }
    else if (arr[mid]>m){
    high = mid-1;
    }
    else{
    low = mid+1;
    }
    }
    return low;
    }

  • @YASWANTHSIDDANATHAM-mi8qk
    @YASWANTHSIDDANATHAM-mi8qk 5 месяцев назад

    Understood thanks striver for amazing video

  • @Manasidas99
    @Manasidas99 Год назад

    Really great series I really appreciate lot thanks bro.

  • @riyabankawat4998
    @riyabankawat4998 3 месяца назад

    understood everything bhaiya .. thankyou

  • @VikashKumar-tg3ot
    @VikashKumar-tg3ot 6 месяцев назад

    This also done on 28 Aril 2024 , Best explain bro

  • @satyasaineelapala570
    @satyasaineelapala570 10 месяцев назад +1

    Most Optimized solution for Floor and Ceil. 1binary search call instead of 2
    pair getFloorAndCeil(vector &a, int n, int x) {
    int start=0;
    int end = n-1;
    int floor =-1;
    int ceil =-1;
    while(start

  • @per.seus._
    @per.seus._ Год назад +1

    UNDERSTOOD❤

  • @rahulreddy3588
    @rahulreddy3588 Год назад

    Understood clearly!

  • @abhisatya23
    @abhisatya23 Год назад

    Understood very well.👌👌

  • @Manishgupta200
    @Manishgupta200 Год назад

    Taught us problems based on lower_bound and upper_bound and bounds problem also, Understood

  • @makingthamilan1592
    @makingthamilan1592 6 месяцев назад

    Understood bhaiya thanks a lot man🙂

  • @sahibahmad3880
    @sahibahmad3880 Год назад

    Understood very well!!!

  • @peanutcoder9361
    @peanutcoder9361 2 месяца назад

    Nicely explained

  • @simarjeetkaur6781
    @simarjeetkaur6781 Год назад +1

    Understood Sir 🌟

  • @SureshKrish-uf6ht
    @SureshKrish-uf6ht 3 месяца назад

    understood binary search thank you sir

  • @sayan_31
    @sayan_31 Год назад

    Amazing explanation . Thank you sir ❤❤

  • @rohitprasad2147
    @rohitprasad2147 Год назад +1

    Awesome 👍

  • @rahulkarki6857
    @rahulkarki6857 Год назад

    love for the knowledge you have respect man!!🤓

  • @mainakdasgupta7130
    @mainakdasgupta7130 3 месяца назад

    int upperBound( vector arr, int n, int x ) {
    int low = 0 ;
    int high = n-1;
    int ans = n ;
    while(low x){
    ans = mid;
    high = mid - 1;
    }
    else{
    low = mid + 1;
    }
    }
    return ans;
    }

  • @AbdurRahman-sp6sm
    @AbdurRahman-sp6sm Год назад

    extran ordinary explanation

  • @khanzarin9960
    @khanzarin9960 Год назад

    great teaching

  • @Manikandan__03
    @Manikandan__03 5 месяцев назад

    According to me u are my god..❤️🙏

  • @striverdaaadi
    @striverdaaadi 10 месяцев назад

    understood, no chance of any doubt

  • @anirudhrajput8282
    @anirudhrajput8282 Год назад

    Really really very much helping aab Mai phod dunga

  • @AbhijeetBose-hj7nd
    @AbhijeetBose-hj7nd 7 месяцев назад

    13:20 (arr[mid] >= target) instead of (arr[mid] >= n)

  • @AnmolGupta-oj4lm
    @AnmolGupta-oj4lm Год назад

    Understood Very Well

  • @VarunKaushal-zx9zq
    @VarunKaushal-zx9zq Год назад

    Understood, Thank you sir!

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 4 месяца назад

    NICE SUPER EXCELLENT MOTIVATED

  • @jyotibrotoroy6930
    @jyotibrotoroy6930 3 месяца назад +2

    isn't the if condition for lower bound should be
    if(v[mid]

  • @k.s5964
    @k.s5964 4 месяца назад +1

    Understood 😊

  • @Dibyadipan
    @Dibyadipan 2 месяца назад

    31:35 understood sir!

  • @raunakkumarmishra1449
    @raunakkumarmishra1449 Год назад

    amazing session

  • @nikhilnallagoni7379
    @nikhilnallagoni7379 Год назад

    great explanation understood

  • @nehathakur40
    @nehathakur40 Год назад

    clear explanation!

  • @k.satish3663
    @k.satish3663 Год назад

    Nice explanation

  • @shaswatpandey2991
    @shaswatpandey2991 Год назад +1

    why your Strivers a to z sheet is not opening. Whenever i'm trying to open it it only shows rounding circle. And only 19 th section is visible

  • @_hulk748
    @_hulk748 10 месяцев назад

    Understood Sir Thankyou Sir🙏🙇‍♂✨

  • @ramanjaneyuluv479
    @ramanjaneyuluv479 Год назад

    understood very well

  • @manavsingh5919
    @manavsingh5919 Год назад

    Thank You... I understood it very well :)

  • @NazeerBashaShaik
    @NazeerBashaShaik 6 месяцев назад

    Understood, thank you.

  • @praveenkanwar953
    @praveenkanwar953 2 месяца назад

    Understood! 🎉

  • @jasmeetsingh5174
    @jasmeetsingh5174 Год назад

    Thank you bro ❤ loved the lecture

  • @AyushSingh-em2il
    @AyushSingh-em2il Год назад +1

    Understood!