Binary Search | BS-18. Allocate Books or Book Allocation | Hard Binary Search

Поделиться
HTML-код
  • Опубликовано: 26 июл 2024
  • Please watch our new video on the same topic: • BS-18. Allocate Books ...
    Check our Website:
    Notes: • BS-18. Allocate Books ...

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

  • @takeUforward
    @takeUforward  10 месяцев назад +3

    Please watch our new video on the same topic: ruclips.net/video/Z0hwjftStI4/видео.html

  • @mohdhasnain3812
    @mohdhasnain3812 3 года назад +109

    Brother honestly saying i haven't seen such a kind of teaching anywhere else.... your expressions along with your hand actions i was able to understand each and every step.. Thanks a lot🔥

    • @arjunyadav-kt5jr
      @arjunyadav-kt5jr Год назад

      exactly my point i had seen his vlogs but to study i came here for the first time, it was so fun understanding the concept

  • @SahilSharma-vl9rk
    @SahilSharma-vl9rk 3 года назад +73

    Now it really feels like we are sitting on our chair and our teacher is teching is on board like in the school
    #Nostalgia

  • @samkr200
    @samkr200 3 года назад +162

    At 23:59, there is a small problem, It's correct in the GitHub code though. It should be pages = arr[i], instead of pages += arr[i];

    • @vaibhavchhabra800
      @vaibhavchhabra800 2 года назад +20

      Right , or make page=0 , then pages+=arr[i];

    • @hadiali5922
      @hadiali5922 2 года назад +4

      @@vaibhavchhabra800 Yep, even I was thinking the same

    • @amishasahu1586
      @amishasahu1586 2 года назад

      Correct !!

    • @ashishverma1382
      @ashishverma1382 2 года назад +1

      good observaton

    • @bong-techie
      @bong-techie 2 года назад

      can you please get me the link of the github where the code is present ?
      thank you

  • @takeUforward
    @takeUforward  3 года назад +66

    Please drop a small feedback on this white board idea of teaching.
    Small typo at end, it will be pages=a[i]
    Follow me at Instagram for all updates: instagram.com/striver_79/​

  • @srikanthreddy9103
    @srikanthreddy9103 Год назад +17

    At 9:05 the low should be the max element of the array. As we cannot divide single book to two persons, the max pages book can be allocated to single person only. So we cannot have the answer less than the maximum of the array. I hope it makes sense :)

  • @ankitkumaryadav562
    @ankitkumaryadav562 3 года назад +55

    at 24:33 Correction else condition ke andar pages = arr[i] hoga mistake se pages += arr[i] leka ja chuka hai

    • @tanmaysinghal8370
      @tanmaysinghal8370 2 года назад +2

      Page to plus hi honge bhai.... Agar same bande jo rha h to

    • @rohitshirur7640
      @rohitshirur7640 2 года назад +1

      @@tanmaysinghal8370 No, brother it will not work when condition is true then new book pages is allotted to that new student and that time we have to make it equals not addition or else the condition will always be true
      try this input and check
      pages = [250 74 159 181 23 45 129 174]
      students = 6

    • @rohitshirur7640
      @rohitshirur7640 2 года назад

      Yes, Brother

    • @tejas7379
      @tejas7379 2 года назад +6

      Ig it should be pages = arr[i] in if condition!!
      As soon as we increase student, we should reset pages

    • @shikharmishra5700
      @shikharmishra5700 2 года назад +1

      abbe chutiye else mein nhi but 2nd if mai pages=arr[i] hoga

  • @ketan_sahu
    @ketan_sahu 3 года назад +1

    No article ever gave me this clear intuition for implementing binary search in an unsorted array. AMAZING!!

  • @nithinshukla4679
    @nithinshukla4679 2 года назад +7

    wow there is literally no one who can explain such a complex problem so easily like you bro. your confidence in explaining it made it very clear. thank you

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

    How could someone possibly think of such an approach?
    Forget 40 mins interview, It would've taken me forever to build such logic.
    Human mind is truly amazing ❤

  • @cenacr007
    @cenacr007 2 года назад +10

    such energy at 3 in the morning, we students are blessed to have people like you.

  • @the_humble_lazy
    @the_humble_lazy 3 года назад +21

    Bro paused at 0:49 to tell u that u look very cool while explaining on the white board........the vibe is so awsm...... honestly speaking

  • @arnabchakraborty246
    @arnabchakraborty246 3 года назад +31

    Bhaiya great video , very crystal clear concept you discussed, and this white board adds 100X more value to us

  • @ekjotsingh9609
    @ekjotsingh9609 3 года назад

    The video helped me realize the power of binary search. Didn't know it could be used in such a way. Thank you.

  • @ShahidHussain-dh3pg
    @ShahidHussain-dh3pg 7 месяцев назад

    Respect for Hard work you are doing.

  • @ankitkumarmahato4318
    @ankitkumarmahato4318 3 года назад +10

    I have a question. In the isPossible function the last line is
    if(allocatedstu>k)
    return false;
    return true;
    this means it will return true even if allocatedstudent is less than k(might be possible) which is not allowed right, because each student should be allocated at least one book.

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

      same doubt ... If you have the solution why he did, Can you please tell me.

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

      I have same doubt. Can someone pls explain??

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

      No one can explain this , even striver bhaiya , that's why he ignore this comment.

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

    I almost gave up on this question, but your explanation exceeded my highest expectations, you deserve a lot more sir, hats off to you.

  • @its_neel_ok
    @its_neel_ok 2 года назад

    the explanation was as clear as a crystal . I could'nt understand the question but now it totally clear. THANKS A LOT !!!

  • @manavpandey2246
    @manavpandey2246 3 года назад +2

    Bhai literally you're best in teaching complex problems into simple solutions want to see more such videos ahead.
    keep doing such good work and motivating students like us.

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

    I have fallen in love with your teaching style! You can teach us whatever you like!

  • @yogendramaarisetty
    @yogendramaarisetty 2 года назад

    Lot of energy in you brother.. Great work!! Keep it up!
    Awesome explanation skills.
    Your visualization and explanation is so GOOD! that I was able to figure out the approach & write code before watching the other half of the video.

  • @kshittizbhardwaj417
    @kshittizbhardwaj417 3 года назад +2

    great explanation as always!!! Also regarding the whiteboard, you just continue with whatever way makes you comfortable but you know our obsession with dark mode and dark IDEs so I personally liked the previous way but as long as you are teaching it's all good!!

  • @MusingsbyAmit
    @MusingsbyAmit 3 года назад

    Anyone one who is looking to learn DSA - this is the best channel I think. It will certainly take u forward.

  • @HallaBhalla
    @HallaBhalla 3 года назад +60

    A slight optimization is that the range lies between Max(arr) - Sum(arr), instead of Min(arr) - Sum(arr)

    • @takeUforward
      @takeUforward  3 года назад +20

      Yes we can do this.. thanks

    • @sandeepnallala48
      @sandeepnallala48 3 года назад +6

      yes bro , we can do this and we can skip the condition at beginning in the for loop because its guaranteed that
      our barrier will always be >= max Of the array, so all array elements individually can fit in the barrier 💛

    • @chandankumarshaw216
      @chandankumarshaw216 3 года назад

      how this possible, because we have to find the minimum no of pages....if we assume that there is only one student then he will get the min from the array and that's the answer

    • @sandeepnallala48
      @sandeepnallala48 3 года назад +2

      @@chandankumarshaw216 we need to find the maximums in all possible allocations, and see which max is actually minimum.. if only ine student then we will allocate all books to him.
      And if there are n Students where n is array size, each student we wil give one book. In each case, one of the student will get tha maxm pages book, and in all the psbl cases , the minimum psbl maximum is only that particular max..
      Dry run with some exmples.
      Crct me striver if iam wrong...

    • @chandankumarshaw216
      @chandankumarshaw216 3 года назад

      @@sandeepnallala48 okk thanks

  • @adishijha8365
    @adishijha8365 3 года назад +2

    It's really inspiring to see that you are putting so much efforts for us. Thank you!

  • @swatinawange5390
    @swatinawange5390 3 года назад +22

    This is amazing and a favorable methodology for learning.❤️💯💯

  • @beginnersbaggage5187
    @beginnersbaggage5187 2 года назад +1

    Well explained brother. I came to your video after watching gfg self paced course video on this topic. I didn't get much clarity of the approach there but the way you explained it, Hats off dude.🤘

  • @kshittizbhardwaj417
    @kshittizbhardwaj417 3 года назад +3

    I have a doubt. I'll be glad if anyone could help. Why are we returning true when allocated students

  • @yashbhange1517
    @yashbhange1517 3 года назад +26

    In IsPossible function ,in If statement , instead of pages+=arr[i] it should be pages=arr[i]

    • @takeUforward
      @takeUforward  3 года назад +9

      Yes mentioned in description as well as pinned comment. Thankyou.

    • @SudhanshuKumar-lp1nr
      @SudhanshuKumar-lp1nr 3 года назад

      Can there be case where all the students are not allocated at least 1 book for a value of mid ???

    • @dutt_2302
      @dutt_2302 3 года назад

      @@SudhanshuKumar-lp1nr hello bro
      int isAllocated(vector& A,int num,int k){
      int count=1,sum=0,flag=0;
      for(int i=0;ik) return 2;
      return 1;
      }
      int Solution::books(vector &A, int B) {
      sort(A.begin(),A.end());
      int sum=0;
      for(int i=0;i

    • @jaisamtani303
      @jaisamtani303 3 года назад

      @@dutt_2302 bro no where in the video it was mentioned to SORT the array, why are you sorting it? Also why are returning 0/1/2 from your helper function.. return true/false and make it simple

  • @marvel438
    @marvel438 2 года назад +15

    Amazing explanation like always but let me clarify time complexity analysis
    Let N = Number of Books
    arr[i] = Number of pages in ith book
    Binary Search Search Space = Low = minimum of arr[i] High = Sum of arr[i] (i -> 0 - N)
    For every search space mid-value we check allocationPossible (O(N) Can be optimised to O(1) with Prefix Sum
    TimeComplexity = O(N * Log(sum of arr - low))
    The the value inside log is the search space. So Log(N) is very wrong.
    Please correct me if I am wrong.
    Thank you

    • @aftabansari3845
      @aftabansari3845 2 года назад +1

      Had the same doubts regarding TC. Seems correct.

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

      You are correct i guees

  • @siddharthchaudhary2320
    @siddharthchaudhary2320 3 года назад +1

    Great approach...never thought one could explain binary search soo easily..

  • @sujalmishra352
    @sujalmishra352 19 дней назад

    Amazing explanation bhaiya💯

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

    Amazing understood the whole approach ,You r just cool.

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

    I think there is one one edge case added into the main method which is - If(number_of_Students > A.length) return -1;

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

    Nice and unique explanation striver thanks a lot

  • @aqua6150
    @aqua6150 2 года назад

    At 24:00 it should be pages = arr[i] otherwise that if loop condition will always be true . As we did student++ so we are reinitializing the vaule of pages as arr[i] as it will calculate the sum for the other student.

  • @jayachandra677
    @jayachandra677 3 года назад

    This is the best intuition behind this problem ever!!! Thanks, bhaiya!!!

  • @sharathnair2790
    @sharathnair2790 3 года назад +5

    This setup is a great upgrade brother.
    P.S. best online lecture of my life❤️

  • @DeepSingh-zs2oi
    @DeepSingh-zs2oi 2 года назад

    Thanks for explaining the search space idea. This gives the intuition for using binary search in such problems.

  • @prateekbaisware7906
    @prateekbaisware7906 3 года назад

    Bhaiya it doesn't looks like you are shifting, it so perfect even than those who are already been teaching on board...Thanks, Bhaiya!!

  • @Amirthanand
    @Amirthanand 2 года назад

    Thanks a lot ! Amazing Content as always ! Keep up the good work bro. Really liked the whiteboard approach :)

  • @rohanchoudhary672
    @rohanchoudhary672 3 года назад +4

    Saw your LinkedIn post sir.
    This white board is surely a boon for our learning.

  • @mohdaasimqureshi7115
    @mohdaasimqureshi7115 3 года назад +7

    this way of teaching is far better than the previous way. Pls continue teaching us in this way only
    btw you explained it exceptionally well👌👌 and thank you for all ur efforts that u r putting in for us🙏
    More power to you bhaiyya 🙌

  • @rajveersingh2056
    @rajveersingh2056 11 месяцев назад

    Big fan bro, Awesome work, But i feel you can binary search the is_possible function as well by using the extra space of N where you store "sum till now".
    this way, it can be done in log(n)*log(n) time
    a = [12, 34, 67, 90]
    sum_a = [12, 46, 113, 203]
    students = 3
    def is_possible(left, b, settled, used_students):
    right = len(a) - 1
    if settled == sum_a[-1]:
    return True, used_students
    while left > 1
    if a[mid] > b:
    return False, used_students
    e_mid = sum_a[mid] - settled
    if e_mid students:
    return False, count
    return True, count
    print(is_possible(left=0, b=b, settled=0, used_students=0))

  • @gouravkumar7459
    @gouravkumar7459 2 года назад

    came here after Binary Search Master Class, although i have done this question but wanted to go through your video once

  • @rigaut-valorant6105
    @rigaut-valorant6105 3 года назад

    Bhaiya you are my inspiration... Please don't stop the good work... There are a lot of videos which teach the basic concepts but very less like you teach the complex algorithms!!... And infact your explanation is one of the best I've ever seen anywhere... Aap teaching line m hi aajao... U are our Baahubali

  • @arnabdeb1486
    @arnabdeb1486 2 года назад

    That was amazing amazing ...just mesmerized please do complete placement series ...I want to follow you till I get placed and definitely beyond in life.

  • @SohilLadhani
    @SohilLadhani 2 года назад +4

    Awesome explanation as always. Keep the videos coming!
    Just a question, can't we take 'low' as the max of the array? Because in any given array, there would be a student (partition) having maximum number of pages, and hence the max sum would be greater than or equal to the max value.
    Taking 'low' as the max of the array would potentially reduce the search space.

  • @vivekojha4807
    @vivekojha4807 3 года назад

    That's a beautiful problem and a beautiful solution to it! Thank you Striver

  • @tunedivya
    @tunedivya 2 года назад

    I really understood this problem well.I like your approach of teaching with a dry run explaination

  • @Raj-rd8qo
    @Raj-rd8qo 3 года назад

    9:09 low value will be maximum in the array . Though it will not affect the solution (little optimization ) .By the way nice explanation.

  • @anveshasharma2726
    @anveshasharma2726 3 года назад +1

    Like always great bhaiya and really love your series. Mujh se pahle ds algo question solve hi nhi hote the but ab mujhe samajh bhi aachi tarah aa jata h bde aaram se. THANKS TO YOU FOR THE SAME

  • @vikashgupta3305
    @vikashgupta3305 2 года назад +1

    Your energy is awesome... your energy while teaching keeps me so much engaged.✌🏻
    Thankyou for this wonderful explanation bhaiya❤️

  • @just4commentsable
    @just4commentsable 2 года назад +1

    Thank you for this video. Incredible explanation. This problem felt so complicated for me at first, I didn't think I could solve it. You really helped simplify this!!!

    • @sachinpathak3084
      @sachinpathak3084 2 года назад

      bro tell me one thing the time complexity of binary search is wrong in the video since the search space is not N but its M = (Sum(arr)-Max(arr)+1)

  • @PramodKumar-sz3od
    @PramodKumar-sz3od Год назад +1

    The initial left limit of the search space should be 90 instead of 12. Which is the value max value present in the array.

  • @arijitdas4560
    @arijitdas4560 3 года назад

    This new technique of teaching making concepts more clear .Thank u for helping us.

  • @harsimran.27
    @harsimran.27 3 года назад

    terrific explanation. Best DSA Teacher I have seen so far on RUclips

  • @ayeshasolanki5386
    @ayeshasolanki5386 2 года назад +4

    Words won't suffice for the way of teaching you provide!!!! ❤

    • @sachinpathak3084
      @sachinpathak3084 2 года назад

      @Ayesha tell me one thing the time complexity of binary search is wrong in the video since the search space is not N but its M = (Sum(arr)-Max(arr)+1)

  • @anmolsingh4026
    @anmolsingh4026 3 года назад

    Bhaiya this is it 🙏 please teach us like this . This is what we wanted . We can grasp the concept easily .

  • @manojbabusa
    @manojbabusa 3 года назад

    I am lazy bird not to write a comment but for whatever reason , I must Appreciate your efforts , energy , dedication and knowledge. Keep this up. Kudos.

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

    I can understand it takes energy to speak so clearly and explain.......thanks. God bless you success.

  • @panhejia
    @panhejia 2 года назад

    You are on fire! great tutorial! Thanks! very well explained! And also a very very tricky problem LOL

  • @kunal_chand
    @kunal_chand 3 года назад +2

    Back to basics. Whiteboard tutorials gives a great vibe for learning!!

  • @itz_me_imraan02
    @itz_me_imraan02 3 года назад +89

    Want a tree series as well🙏

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

    at 9:13, when deciding the minimum range, I think it should be the maximum of the given books array instead of the minimum from the array.
    when students and number of books are equal, the answer will be the maximum among the pages of the book and not the minimum one.

  • @prithvichetty15
    @prithvichetty15 3 года назад +1

    Best Explaination Whiteboard se concept aur clear hogaya waiting for your complete sde sheet to complete. ❤👌👍

  • @miragranger4685
    @miragranger4685 2 года назад

    Understood the approach clearly. Thanks a lot for this amazing explanation ❣💯

  • @radhe1335
    @radhe1335 3 года назад +15

    Energy & explanation 😍🔥

  • @RohitSharma-bq1px
    @RohitSharma-bq1px Год назад

    one of the best video i have seen with the kind of explation and dedication
    Thanku for this

  • @viveksingh6400
    @viveksingh6400 3 года назад +1

    Amazing communication skills and an amazing personality, keep it up, man :)

  • @tanishaguptacse2032
    @tanishaguptacse2032 11 месяцев назад

    You made the complexity so easy... Thank you so much for the explanation

  • @nileshsinha7869
    @nileshsinha7869 3 года назад

    Understood❤❤ the thing u did every time while saying "minimal" 😂 khub bhalo dada 👌💕

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

    I would believe the minimumValue should be the maximum of (arr[I]) , For example -> A = [5, 17, 100, 11]B = 4
    here low=100, and high=SumofArr[I].

  • @subhamchakraborty3173
    @subhamchakraborty3173 3 года назад

    awesome explanation sir ,i almost forgotten the approach as i did that problem two years ago .

  • @krishnasinghal14
    @krishnasinghal14 8 месяцев назад +1

    9:00 - the low should be the max element of the array. the max pages book can be allocated to single person only. So we cannot have the answer less than the maximum of the array.

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

    UNDERSTOOD.... !!!
    Long life to the LEGEND.... :)

  • @adarshbadagala
    @adarshbadagala 2 года назад

    Thank you so much Bhayya, you hard work is immeasurable. Please keep doing videos and keep teaching us

  • @ashutoshsanodia5690
    @ashutoshsanodia5690 3 года назад +1

    Please carry on these kind of videos on white board ! Really understood !

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

    Understood! Super awesome explanation as always, thank you very much!!

  • @lsrinivasamurthy
    @lsrinivasamurthy 2 года назад

    Neat and clean explanation with crystal clarity that too with a very high energy levels...

  • @adityaramakrishnan969
    @adityaramakrishnan969 3 года назад

    How can anyone be so fresh while teaching at 3AM?!!! Thumbs up bro

  • @abhishekvishwakarma9045
    @abhishekvishwakarma9045 3 года назад

    I always like pen and board teaching this makes it really awesome 🔥🔥

  • @swapnilkumar4411
    @swapnilkumar4411 3 года назад

    Bhai ek dum se 🔥🔥🔥 2-3 videos me samajh me nahi aa Raha tha.. aapke videos se pura concept pata lag gaya

    • @takeUforward
      @takeUforward  3 года назад

      2-3 videos dekhna hi kyu h jb TUF ka video h XD

  • @arnabdutta4662
    @arnabdutta4662 2 года назад +1

    Initially we can assign low = max(of the array elements) and high = sum(of all the array elements)
    Because it will be the minimum of maximum allotted pages.
    And I have one more doubt isn't the time complexity of the binary search - Log(sum_of_all_elements - min_element) or Log(sum_of_all_elements - max_element) ??

  • @rishavraj1536
    @rishavraj1536 3 года назад +1

    bhaiya maaja aa gya ekdum feeel aaa gyaa bhaiyaaaa ekdum ..loved white board...bhaiya aage ki videos white board pe bnana bhaiya

  • @gaishiya7696
    @gaishiya7696 3 года назад

    bruh I honestly don't know how someone can devote himself completely to teaching!!!

  • @shivangisrivastava1158
    @shivangisrivastava1158 3 года назад

    So much more connected to your videos now. This is the best. Pls pls continue on white board💯🤩 You are exceptional bro

  • @jaiminsolanki5478
    @jaiminsolanki5478 3 года назад +1

    Raj Bhaiya, the energy with which the video is made, it was so engaging ,so crisp and clear! Thankyou so much brother!

  • @tarungarg4489
    @tarungarg4489 2 года назад

    Brilliant explanation brother. You series is really helping me a lot for the preparation. Just one query in the second If statement of isPossible function should not we set pages to the element value otherwise after this condition every time it will increase the count. Any thoughts on this?

  • @anupreetkaur3696
    @anupreetkaur3696 2 года назад

    I am your big fan. Lots of Love and Respect. Your teaching skills are upto the mark

  • @deepakkamat7728
    @deepakkamat7728 3 года назад +1

    The teaching way is Phenomenal 🔥🔥

  • @Shubham-wm9oj
    @Shubham-wm9oj 3 года назад +22

    Thanks for the genuine efforts Bhaiya !! I have watched your all the videos and learnt a lot from your sde series, your graph series is like a gem !
    bhaiya according to me.... teaching on pad is much more easier and effective 😅!!
    all the best bhaiya

    • @takeUforward
      @takeUforward  3 года назад +18

      Yes easier but usually in pad, i cannot show hand expressions, which in turn makes it very tough to communicate my thought.

    • @shivamkapil8116
      @shivamkapil8116 3 года назад

      @@takeUforward bhyaiya please make more videos on algorithms whenever u get time❣️❣️..it's really helpful

  • @ruiqiu9947
    @ruiqiu9947 8 месяцев назад

    Dude made the funniest algo tutorial!=

  • @sourabhkumar6194
    @sourabhkumar6194 2 года назад +1

    those small tips about interview like not telling dp solution to save time makes this channel special

  • @dheerajkumark7165
    @dheerajkumark7165 3 года назад

    explanation is great!!And your effort to explain is awesome..!

  • @mihirbhasin1970
    @mihirbhasin1970 3 года назад +1

    Bhai if I was not in 3 year I would have been Google ready kya he revision horha op stuff brother keep it up..

  • @vasujain1970
    @vasujain1970 3 года назад

    Beautiful explanation... Love this video!

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

    The array can be unsorted as well. The approach still works fine.
    BTW, the tutorial is best 💥

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

    Just one thing, you need to consider the maximum array value because the size of maximum pages required in any permutation cannot be less than the maximum no of pages of one single book.

  • @sohamsaha5398
    @sohamsaha5398 3 года назад

    Saw the trial in the telegram grp
    Was excited to see the video
    After giving my college exam I sat to watch the video.
    It was worth the wait ♥️