BS-16. Kth Missing Positive Number | Maths + Binary Search

Sdílet
Vložit
  • čas přidán 22. 06. 2023
  • Problem Link: bit.ly/3p30UBg
    Notes/C++/Java/Python codes:
    We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
    Full Course: bit.ly/tufA2ZYt
    You can follow me across social media, all my handles are below:
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    0:00 Introduction of Course

Komentáře • 240

  • @sauravchandra10
    @sauravchandra10 Před 11 měsíci +296

    Is it only me or the brute force was difficult to understand?

  • @Anony.musk01
    @Anony.musk01 Před 11 měsíci +73

    correction: 21:18 time complexity should be O(logN)* . Great explanation though

  • @rounaq_khandelwal
    @rounaq_khandelwal Před 11 měsíci +116

    Correction, @striver, at 2:45 the answer for arr=[5,7,10,12] and k=6 is 8 and not 7. Thanks for these series

  • @varunpalsingh3822
    @varunpalsingh3822 Před 11 měsíci +38

    Optimal one is just out of the box kind of thing ❤

    • @shreyxnsh.14
      @shreyxnsh.14 Před 4 měsíci +4

      yeah, you can only solve this question in the interview if you have solved it before

    • @Ayush37262
      @Ayush37262 Před 3 měsíci +2

      ​@@shreyxnsh.14 Thanks bhai,
      Solve nhi ho rha tha aur easy tag dekh kar depressed feel ho rha tha...

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

      @@Ayush37262 Many easy problems are actually hard so don't be demotivated if you can't solve an easy rated problem, it's because it's not easy at all.

  • @aryansinha1818
    @aryansinha1818 Před 6 měsíci +11

    No doubt you explain nicely, everything gets crystal clear after going through your lecture video, but I wonder how do you come up with this type of solution on your own. It's like scientist making some mind boggling discoveries.

  • @cinime
    @cinime Před 11 měsíci +4

    Understood! Super fantastic explanation as always, thank you so so much for your effort!!

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

    you are far better than any of the comedians and so called celebrities . Thanks!

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

    I have never seen anything fantastic than this , I already enrolled in paid DSA course but trust me guys if I found out this channel and dsa sheet before that
    i have never taken the paid dsa course
    this is just awesome

  • @purushottam108
    @purushottam108 Před dnem

    simple problem koo completed or comopleted problem koo simple, bana koi aap sai sikhai, you are master of dsa🤩🤩🤩🤩❤❤❤❤❤❤❤❤

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před 11 měsíci +1

    Very beautiful explained. With logic

  • @alessandrocamilleri1239
    @alessandrocamilleri1239 Před 11 měsíci +11

    Thank you for the explanation.
    Two edge cases that should improve running time:
    if (k < vec[low]) return k;
    if (k > vec[high] - n) return k + n;

  • @user-ti3bd8mp1w
    @user-ti3bd8mp1w Před 11 měsíci

    understood
    Thank you striver for such an amazing explanation

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

    Pure Genius Explanation....

  • @sayakghosh5104
    @sayakghosh5104 Před 11 měsíci +13

    For [4,7,9] and k = 3, answer will be simple that is 3, bcz k < arr[0] for all these cases we can directly return k.
    And at the end we can use arr[high] like:-
    " int diff = (arr[high] - high + 1);
    int miss = k - diff;
    return arr[high] + miss; "

    • @takeUforward
      @takeUforward  Před 11 měsíci +5

      Yes you can do this as well

    • @003_baneshubhamvijay3
      @003_baneshubhamvijay3 Před 11 měsíci +5

      int diff = arr[high] - (high + 1);
      int miss = k - diff;
      return arr[high] + miss;
      A small correction in parenthesis**

  • @lostcyrus8578
    @lostcyrus8578 Před 11 měsíci +2

    Bhaiya you are doing a great work ❤

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

    great explanation for low+k.

  • @nousadali4808
    @nousadali4808 Před 3 měsíci +2

    After Watching 3 time I Finally understand

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

    Loved the explanation.

  • @santhosh7042
    @santhosh7042 Před 7 měsíci +1

    🤯 didn't thought for the binary search approach

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

    Thanks a lot Bhaiya . Crystal Clear

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

    The subtlety in the algorithm is not considering (arr[mid] - (mid+1) == k) coz if we hit that we have no way to figure out the answer(take k=1 as example and your arr[mid]-(mid+1) matches with k, that is why there is no condition for equals k. Idea is to move high to the index where the missing numbers are lesser than k, that way we get the low end and all we got to do is add the remaining. would have been good if this point was insisted coz if you forget the algorithm and try to write from scratch thinking oh it's binary search, you're gonna get stuck in an interview.

  • @sayyidzyanashraf
    @sayyidzyanashraf Před dnem

    Nice explanation Sir ❤❤❤

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

    nicely explained bro ....thanks

  • @AnmolGupta-oj4lm
    @AnmolGupta-oj4lm Před 10 měsíci

    Understood Very Well!

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

    For brute force in the array 5,7,10,12 k=6.
    For 5 -> k=7, 7 -> k=8, 10 exceeds the value hence k becomes 8 and 8 should be returned. Hope this helps!

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

    how beautifully u explained the brute force. top level intuition👑.

  • @ReD4eva94
    @ReD4eva94 Před 4 měsíci

    Brilliant. Thanks!

  • @lucario404
    @lucario404 Před 11 měsíci

    @takeUforward maybe you can update the time complexity in description for binary search as O(log2N)

  • @user-xe2jt5vl5g
    @user-xe2jt5vl5g Před 7 měsíci

    i knew there was better algo than quick select thanks

  • @mikedelta658
    @mikedelta658 Před 4 měsíci

    Fantastic. Thank you.

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

    I coudln't think this way !! best solution

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

    Understood, thank you.

  • @pulkitjain5159
    @pulkitjain5159 Před 11 měsíci

    amazing solution , maja agya

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

    Happy to solve without watching solution , took 4hrs to figure out approach and code it . Yeah happy me🙂

  • @shamanthhegde2820
    @shamanthhegde2820 Před 3 měsíci

    Thank you so much!! this is one of the trickiest binary search problem which I was able to figure out on my own.
    class Solution {
    public int findKthPositive(int[] arr, int k) {
    if(k < arr[0]) {
    return k;
    }
    int low = 0;
    int high = arr.length-1;
    while(low

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

    you are the best brother

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

    Helped a lot 🙂🙂😌

  • @tridibeshmisra9426
    @tridibeshmisra9426 Před 3 měsíci

    good question .. maza aya karke

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

    Understood Bhaiya ..!!

  • @Shunya_Advait
    @Shunya_Advait Před 9 měsíci +1

    Understood Sir.

  • @rahulkarki6857
    @rahulkarki6857 Před 11 měsíci

    Totally dependent on you bro ❤

  • @saketjaiswal9381
    @saketjaiswal9381 Před 10 měsíci +2

    21:23 error time complexity will be o(log base 2 n) that is o(log n)

  • @samreenimam8608
    @samreenimam8608 Před 5 měsíci

    bar bar dekho... hazaar bar dekho or practice kro is the key here

  • @dank7044
    @dank7044 Před 6 měsíci

    the brute force solution is beautiful.\

  • @prakharsahu5778
    @prakharsahu5778 Před 11 měsíci +1

    when are you planning to complete this series ?

  • @629simran6
    @629simran6 Před 11 měsíci

    thank you so much sir

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

    Awesome Sir......

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l Před 4 měsíci

    Thank you Bhaiya

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

    An easier brute force code:-
    int missingK(vector < int > vec, int n, int k) {
    // Write your code here.
    int i=0,j=1,missing=0;
    while(missing!=k){
    if(vec[i]!=j){
    missing++;
    j++;
    }
    else{
    i++, j++;
    }
    }
    j--; //We did this because j moved 1 forward because of the if statement. So to get our answer, we need to move j backward by 1.
    return j;
    }

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

      we learn here how to implement binary search not only how to solve in this specific problem.

    • @rishabh_pant
      @rishabh_pant Před 6 měsíci

      its perfect

    • @shrinikclub8883
      @shrinikclub8883 Před 6 měsíci

      there is a potential issue in your code. The j-- statement before returning the result might cause incorrect results. If the loop exits because missing equals k, then j should already represent the k-th missing positive number. Decrementing it before returning could lead to incorrect results.

  • @user-is6ky7pp2n
    @user-is6ky7pp2n Před 10 dny

    Understood !! 😎😎

  • @YourCodeVerse
    @YourCodeVerse Před 5 měsíci

    Understood✅🔥🔥

  • @culeforever5408
    @culeforever5408 Před 7 měsíci +1

    understood 😇

  • @hashcodez757
    @hashcodez757 Před 4 měsíci

    Understood!!

  • @Amine-yq7jg
    @Amine-yq7jg Před 11 měsíci

    understood!

  • @dogwoofwoof8154
    @dogwoofwoof8154 Před 5 měsíci +1

    after a good brainstorming session it's time to see the video :)

    • @Ayush37262
      @Ayush37262 Před 3 měsíci

      Did you solved the question before watching solution??

    • @dogwoofwoof8154
      @dogwoofwoof8154 Před 3 měsíci

      @@Ayush37262 yep

    • @Ayush37262
      @Ayush37262 Před 3 měsíci

      @@dogwoofwoof8154 You are Batman!

  • @badrirao6472
    @badrirao6472 Před 19 dny +1

    why cant we use arr[mid] -- (mid + 1 ) formula to calculate the missing number instead of the formula arr[high ] -- (high + 1) ??

  • @md.imrankhan6912
    @md.imrankhan6912 Před 9 měsíci

    Legendary Boss

  • @senseiAree
    @senseiAree Před 8 měsíci

    Understood ❤

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

    Understooooooood!

  • @firebout7675
    @firebout7675 Před 5 měsíci

    understood

  • @her_soulmate
    @her_soulmate Před 5 měsíci

    Understand 🎉

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

    Understood

  • @bhanusingh4351
    @bhanusingh4351 Před 9 měsíci

    at 21:17 , time complexity would be O(logn) [ just a human error]

  • @ankitgupta7467
    @ankitgupta7467 Před 8 měsíci

    Hey Bhagwan , The BS intuition great

  • @mySpace940
    @mySpace940 Před 8 měsíci

    how in explanation high

  • @dipanshuraj7868
    @dipanshuraj7868 Před měsícem +1

    Literally, I spent one day trying to solve the brute force but when I saw the brute force in the video I was shocked how is it possible?😐😐

  • @ritikanand3425
    @ritikanand3425 Před 11 měsíci

    Bhaiya the GfG link to the question in a2z DSA Sheet the question is bit different to what explained in video for example in the test case n = 3 , k=1 {32 , 59 ,77} the first missing number is 33

  • @AS-gf3ci
    @AS-gf3ci Před 10 měsíci +3

    Both the solutions are difficult to grasp at first go. It needs another revisit for sure

    • @shreyxnsh.14
      @shreyxnsh.14 Před 4 měsíci

      faxx, how is your dsa prep now btw?

  • @preritphoenixgupta330
    @preritphoenixgupta330 Před 11 měsíci +17

    Hi Striver, I'm a final year student and our placement season is coming soon can you give some tips. Also, there are some topics which I need to work upon like Stack n Queues when can we expect those series/ playlist from You. I like your way of teaching and approach, I know you are caught up with a lot of work but if you could bring those series soon or suggest some alternatives that would be great.

    • @pulkitjain5159
      @pulkitjain5159 Před 11 měsíci +2

      bhai stack m mujhe bhi dikkat aai thi par bhaiya ka largest histogram wala solution dekhna , dono parts , then leetcode ke question lagana nahi aaye toh uske solution dekhle , fhir aane lagega comfidence m bhi yahi kara hu bhai

    • @preritphoenixgupta330
      @preritphoenixgupta330 Před 11 měsíci

      @@pulkitjain5159 okay, thanks bhai

    • @saketsoni2587
      @saketsoni2587 Před 11 měsíci +1

      stack me sirf monotonic stack wala ek concept hai jo baar baar dikh jata sawalo me, wo largest histogram wale me covered hai

    • @preritphoenixgupta330
      @preritphoenixgupta330 Před 11 měsíci

      @@saketsoni2587 Ok Thanks

  • @ShahNawaz-cx3pi
    @ShahNawaz-cx3pi Před dnem +1

    ******** ONE OBSERVATION *********
    what if the number of missing elements = k , i.e.
    int missing = arr[mid]-(mid+1);
    missing == k
    can we write this:
    if(missing

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

    why this question is tagged easy on leetcode

    • @VivekYadav-uy9ts
      @VivekYadav-uy9ts Před 6 měsíci

      Bacause of the contraints, there the array size is given between 1 to 1000 only, so you can just simply traverse the whole array and can find k'th missing element!

  • @rhul0017
    @rhul0017 Před 8 měsíci

    intuition behind incrementing k is not clear, are we assuming kth element is k inititally and why incrementing k leads to answer, i understood the code but intuition is not clear

  • @dreamyme543
    @dreamyme543 Před 8 měsíci

    Understood🙃

  • @pihus3498
    @pihus3498 Před 11 měsíci

    understoood :)

  • @VishalGupta-xw2rp
    @VishalGupta-xw2rp Před 8 měsíci

    13:10 opposite polarity

  • @anshulrawat3458
    @anshulrawat3458 Před 8 měsíci

    bhaiya bs ques tha ki : jaise aap figure out krrh ho ki 1,2,3,4 ideally 4,7,9,10 arr m hone chaiye the. so inko substract kre to missing number milenge. iska logic nhi bnra dry run krte wkt. mtlab wo missing number maths se aara h but kaise aaya ye nhi feel ara..

  • @shubhamsingh4701
    @shubhamsingh4701 Před 10 měsíci +1

    just one question though, we are finding missing via arr[mid]-mid+1; why are we using using arr[high] ?? like fore expression arr[high]-high+1 ?? arr[high]+more ??

    • @magdeline1207
      @magdeline1207 Před 8 měsíci

      the purpose of binary search using mid is to let high point to idx in arr where there are fewer number of missing int than k, for eg. arr[high] point to idx where there are 3 missing before that idx, and we just use arr[high]+ (k-missing) to find the answer, since (k-missing) is the number of missing int on the right of arr[high]

  • @user-xp3hf2mb2u
    @user-xp3hf2mb2u Před 3 měsíci

    at 19:45 sir have taken value of "more" as arr[high]-high-1 but should it be arr[high]-high+1?.....can anyone plz explain??

  • @ambaradhikari7425
    @ambaradhikari7425 Před 11 měsíci +2

    Bro by when your whole dsa sheet will be covered, placement coming up?any eta approx?

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

    what if the input arr[]=[2] and k=1 ? According to this logic i think answer is 2 but actully it is 1....

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

    Optimal code takes much time to understand

  • @amitfritz11
    @amitfritz11 Před 11 měsíci +1

    I just wonder this is only first video bit difficult to understand both brute force and optimal.

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

    GodLike!

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

    your brute force solution is optimal solution for me 🤣🤣

  • @rohandas6298
    @rohandas6298 Před 11 měsíci +2

    int missingK(vector < int > vec, int n, int k) {

    int low=0,high=n-1;
    while(low

  • @amrishshah8982
    @amrishshah8982 Před 9 měsíci

    For brute force this case 2,5,6,7,9 and k=5 will fail.. correct me..

  • @souhardayasaha
    @souhardayasaha Před 11 měsíci

    bhaiya iska link mein galat question ka code hai aur sheet mein link bhi nahi hai

  • @CodeMode9313
    @CodeMode9313 Před 9 měsíci

    Habibi batai toh mast hai tum ....correction :) @ 21:18 time stamp ie TC be logn

  • @shivanshumishra0560
    @shivanshumishra0560 Před 11 měsíci +1

    Bhaiya ek request thi app se🙏 ... A2Z DSA Sheet me GFG ke question ka link hatne ke karan bahut problem ho raha hai , abhi mai apke ke sheet se approx 250 problem GFG plateform pe kar chuka hoon but ab sheet se GFG ke problems ka link hatne ke karan question ko revist karne me bahut problem ho raha hai ... I think aap mere problem ko samajh pa rahe honge ...So please bhaiya 🙏🙏🙏🙏 GFG ke problems ka bhi link add karva digiye 🙏🙏🙏🙏

  • @umangkumar2005
    @umangkumar2005 Před 4 měsíci

    int missingK(vector < int > arr, int n, int k) {

    int low=0;
    int high=n-1;
    int mid=low+(high-low)/2;
    while(low

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

    TC of bs solution is logn

  • @rethickpavan4264
    @rethickpavan4264 Před 7 dny

    Before u see this video think with different examples by how can we eliminate one half (FINISHED) . If you understand the opposite polarity concept (unless u can't without paper pen ) this question is piece of cake ; I took 1 hr to solve :)

  • @rohanmadiratta6421
    @rohanmadiratta6421 Před 4 měsíci

    I didnt get it
    for array 2 3 4 7 11
    once we exit the while loop our arr[high] is 7 but missing is pointing to the last index so isnt missing 11 - i -1 = 6?
    how r u considering missing equal to 3?

    • @harshilpatel3205
      @harshilpatel3205 Před 4 měsíci

      Bro if you take 11 for consideration to calculate the final answer( kth value ) , so in that case it won't give you the correct answer( kth value) , that's why he has taken 7 and calculate the missing number further to find the kth value :)

  • @aryansinha1818
    @aryansinha1818 Před 6 měsíci

    Why does it not work on gfg?

  • @codeman3828
    @codeman3828 Před 4 měsíci

    Undertsood

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

    bruhh howTf did you thibk of that solution! It was insane.

  • @jeet-smokey
    @jeet-smokey Před měsícem

    Understood. But it is difficult.

  • @user-gf1ew5wd1p
    @user-gf1ew5wd1p Před 10 měsíci

    Another related approach
    class Solution {
    private:
    int possible(int low ,int mid,unordered_map& mp,int k){
    int count =0;
    for(int i=low;i

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

  • @sujalgupta6100
    @sujalgupta6100 Před měsícem +1

    there is always some question like this whose logic doesn't make sense.