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

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

Пікірлер: 277

  • @sauravchandra10
    @sauravchandra10 Жыл бұрын

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

  • @bhalulal5947

    @bhalulal5947

    Жыл бұрын

    Same here bro I was frst shocked 😅

  • @tarunprajapati7642

    @tarunprajapati7642

    Жыл бұрын

    For me also

  • @kiranmoura2974

    @kiranmoura2974

    Жыл бұрын

    Yes it is difficult

  • @elizabethr5161

    @elizabethr5161

    Жыл бұрын

    even I feel difficult.

  • @dhruv412

    @dhruv412

    Жыл бұрын

    same

  • @Anony.musk01
    @Anony.musk01 Жыл бұрын

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

  • @rounaq_khandelwal
    @rounaq_khandelwal Жыл бұрын

    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

  • @Bharat_Rider

    @Bharat_Rider

    10 ай бұрын

    😂😂 this correction helped me so much🤣

  • @sidharthjain2129

    @sidharthjain2129

    9 ай бұрын

    TBH it did help understand the brute force. Thanks mate.

  • @rounaq_khandelwal

    @rounaq_khandelwal

    9 ай бұрын

    @@sidharthjain2129 welcome!

  • @aryansinha1818
    @aryansinha18187 ай бұрын

    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 Жыл бұрын

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

  • @karanhaldar755
    @karanhaldar7553 ай бұрын

    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

  • @kirannagulakonda3443
    @kirannagulakonda34432 ай бұрын

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

  • @alessandrocamilleri1239
    @alessandrocamilleri1239 Жыл бұрын

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

  • @Dev-x3b

    @Dev-x3b

    Жыл бұрын

    💯

  • @prathamsharma4802
    @prathamsharma48029 ай бұрын

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

  • @swayamsharma7614

    @swayamsharma7614

    12 күн бұрын

    in what sense you are comparing ?

  • @varunpalsingh3822
    @varunpalsingh3822 Жыл бұрын

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

  • @shreyxnsh.14

    @shreyxnsh.14

    6 ай бұрын

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

  • @Ayush37262

    @Ayush37262

    5 ай бұрын

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

  • @saswatrath4646

    @saswatrath4646

    3 ай бұрын

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

  • @bhaskararya9112
    @bhaskararya911213 күн бұрын

    Awesome explanation man, after some self dry run of few examples, I completely got the concept. Thanks!

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Жыл бұрын

    Very beautiful explained. With logic

  • @abhishekshinde5375
    @abhishekshinde53759 күн бұрын

    brute force assume you have an empty array and we have some k value, say k = 4the missing number, so in this case the missing value would be 4 Case 1:- where the value at a index in the array now just consider the empty array again and we added a number smaller than k to it, say 2, so now when we again try to find the 4th missing number we would get it as 5 ( as 1 3 4 5 , as 2 already present in the array, hence the missing value shifts by one position ahead and 5 is the 4th missing value), hence whenever we get a number in the array smaller than k, the kth missing value shifts by position ahead Case2 :- where value at a index in array > k now consider empty array again and we added a number greater than k, assume k = 4 and we added 7 to it, here the kth missing element will be 4 itself, as even though seven was added - it indicated that the array might or might not contain the first 6 numbers and as the k = 4 value is lesser than 7, so this kth value would also come under missing value, and as 7 > k, so no effect on k occur. So k is the missing element

  • @user-ti3bd8mp1w
    @user-ti3bd8mp1w Жыл бұрын

    understood Thank you striver for such an amazing explanation

  • @santhosh7042
    @santhosh70428 ай бұрын

    🤯 didn't thought for the binary search approach

  • @ipshitatandon5134
    @ipshitatandon513427 күн бұрын

    Crazy derivation sir! thank youu for the series

  • @sagarsrivastava1236
    @sagarsrivastava12364 ай бұрын

    Pure Genius Explanation....

  • @girikgarg8
    @girikgarg83 күн бұрын

    Great explanation !

  • @lostcyrus8578
    @lostcyrus8578 Жыл бұрын

    Bhaiya you are doing a great work ❤

  • @sayakghosh5104
    @sayakghosh5104 Жыл бұрын

    For [4,7,9] and k = 3, answer will be simple that is 3, bcz 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

    Жыл бұрын

    Yes you can do this as well

  • @003_baneshubhamvijay3

    @003_baneshubhamvijay3

    Жыл бұрын

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

  • @sukhii0220

    @sukhii0220

    Ай бұрын

    how does this works ? where we would write this after when high cross low ?

  • @sukhii0220

    @sukhii0220

    Ай бұрын

    @@003_baneshubhamvijay3 how does it working ? it gives 6 if we apply in [4, 7,9] and k=3

  • @ayushmanpaul3931
    @ayushmanpaul393111 ай бұрын

    great explanation for low+k.

  • @adityamaurya6646
    @adityamaurya6646Күн бұрын

    This guy's just incredibleee!

  • @rushilvyas9869
    @rushilvyas98692 ай бұрын

    Loved the explanation.

  • @immrhrr
    @immrhrr5 күн бұрын

    thankfully I was able to do it by my own. I was able to think this approach myself

  • @happyteen7195
    @happyteen71959 ай бұрын

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

  • @yoMisterWhyte
    @yoMisterWhyte10 ай бұрын

    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.

  • @harshitpandey8304

    @harshitpandey8304

    10 ай бұрын

    I agree

  • @saswatrath4646

    @saswatrath4646

    3 ай бұрын

    great observation, it will help me memorize this algorithm better, thanks!

  • @purushottam108
    @purushottam108Ай бұрын

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

  • @AnmolGupta-oj4lm
    @AnmolGupta-oj4lm11 ай бұрын

    Understood Very Well!

  • @user-or5oz1pk2x
    @user-or5oz1pk2x2 ай бұрын

    Thanks a lot Bhaiya . Crystal Clear

  • @jaganmohan2198
    @jaganmohan21983 ай бұрын

    nicely explained bro ....thanks

  • @ReD4eva94
    @ReD4eva946 ай бұрын

    Brilliant. Thanks!

  • @lucario404
    @lucario404 Жыл бұрын

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

  • @dipanshuraj7868
    @dipanshuraj78682 ай бұрын

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

  • @mittal_aaditya
    @mittal_aaditya4 ай бұрын

    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!

  • @pulkitjain5159
    @pulkitjain5159 Жыл бұрын

    amazing solution , maja agya

  • @Gaurav_Tripathi_
    @Gaurav_Tripathi_8 ай бұрын

    Understood Bhaiya ..!!

  • @lokeshnegi5051
    @lokeshnegi50514 күн бұрын

    awesome explanation

  • @samreenimam8608
    @samreenimam86086 ай бұрын

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

  • @Akhilkumar0024
    @Akhilkumar002415 күн бұрын

    this question scared the living daylights out of me, couldn't solve it even brute force was this really an easy question

  • @_SahilShah
    @_SahilShah6 күн бұрын

    letsss goooo!! Understood

  • @mikedelta658
    @mikedelta6585 ай бұрын

    Fantastic. Thank you.

  • @thedon713
    @thedon7138 ай бұрын

    you are the best brother

  • @user-xe2jt5vl5g
    @user-xe2jt5vl5g8 ай бұрын

    i knew there was better algo than quick select thanks

  • @sayyidzyanashraf
    @sayyidzyanashrafАй бұрын

    Nice explanation Sir ❤❤❤

  • @rahulkarki6857
    @rahulkarki6857 Жыл бұрын

    Totally dependent on you bro ❤

  • @NazeerBashaShaik
    @NazeerBashaShaik3 ай бұрын

    Understood, thank you.

  • @Shunya_Advait
    @Shunya_Advait11 ай бұрын

    Understood Sir.

  • @tridibeshmisra9426
    @tridibeshmisra94265 ай бұрын

    good question .. maza aya karke

  • @harshdiwase1941
    @harshdiwase19413 ай бұрын

    I coudln't think this way !! best solution

  • @mzia1210
    @mzia121011 ай бұрын

    Helped a lot 🙂🙂😌

  • @seekingsoul871
    @seekingsoul8717 күн бұрын

    Best explanation

  • @prakharsahu5778
    @prakharsahu5778 Жыл бұрын

    when are you planning to complete this series ?

  • @YourCodeVerse
    @YourCodeVerse7 ай бұрын

    Understood✅🔥🔥

  • @sarangkumarsingh7901
    @sarangkumarsingh79012 ай бұрын

    Awesome Sir......

  • @U2011-n7w
    @U2011-n7w9 ай бұрын

    why this question is tagged easy on leetcode

  • @VivekYadav-uy9ts

    @VivekYadav-uy9ts

    7 ай бұрын

    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!

  • @culeforever5408
    @culeforever54088 ай бұрын

    understood 😇

  • @saketjaiswal9381
    @saketjaiswal938111 ай бұрын

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

  • @md.imrankhan6912
    @md.imrankhan691210 ай бұрын

    Legendary Boss

  • @harshitjaiswal9439
    @harshitjaiswal943911 ай бұрын

    Understooooooood!

  • @dank7044
    @dank70447 ай бұрын

    the brute force solution is beautiful.\

  • @dogwoofwoof8154
    @dogwoofwoof81546 ай бұрын

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

  • @Ayush37262

    @Ayush37262

    5 ай бұрын

    Did you solved the question before watching solution??

  • @dogwoofwoof8154

    @dogwoofwoof8154

    5 ай бұрын

    @@Ayush37262 yep

  • @Ayush37262

    @Ayush37262

    5 ай бұрын

    @@dogwoofwoof8154 You are Batman!

  • @629simran6
    @629simran6 Жыл бұрын

    thank you so much sir

  • @Amine-yq7jg
    @Amine-yq7jg Жыл бұрын

    understood!

  • @shamanthhegde2820
    @shamanthhegde28204 ай бұрын

    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 return k; } int low = 0; int high = arr.length-1; while(low

  • @sukhii0220

    @sukhii0220

    Ай бұрын

    this existingdifff is the difference between the range we find out ?

  • @mySpace940
    @mySpace9409 ай бұрын

    how in explanation high

  • @senseiAree
    @senseiAree9 ай бұрын

    Understood ❤

  • @user-is6ky7pp2n
    @user-is6ky7pp2nАй бұрын

    Understood !! 😎😎

  • @ankitgupta7467
    @ankitgupta746710 ай бұрын

    Hey Bhagwan , The BS intuition great

  • @amitfritz11
    @amitfritz11 Жыл бұрын

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

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l5 ай бұрын

    Thank you Bhaiya

  • @AS-gf3ci
    @AS-gf3ci11 ай бұрын

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

  • @shreyxnsh.14

    @shreyxnsh.14

    6 ай бұрын

    faxx, how is your dsa prep now btw?

  • @K_EN_VisheshSaini
    @K_EN_VisheshSaini9 ай бұрын

    An easier brute force code:- int missingK(vector 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

    8 ай бұрын

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

  • @rishabh_pant

    @rishabh_pant

    7 ай бұрын

    its perfect

  • @shrinikclub8883

    @shrinikclub8883

    7 ай бұрын

    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.

  • @ritikanand3425
    @ritikanand3425 Жыл бұрын

    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

  • @Kaushik846
    @Kaushik846 Жыл бұрын

    Understood!!

  • @nousad-ali-0
    @nousad-ali-04 ай бұрын

    After Watching 3 time I Finally understand

  • @dhamotz4737

    @dhamotz4737

    16 күн бұрын

    can you explain why are we using missing=arr[I]-[I+1] , for finding the missing numbers ? cause this formulae is setting in the question but as I am customising the values in the question the missing numbers don't come? can u give some test cases in which you are getting answer for this formula only sp that I can approve that this formulae works for every possible cases not only what he has given there

  • @ShahNawaz-cx3pi
    @ShahNawaz-cx3piАй бұрын

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

  • @akworld2739
    @akworld27399 ай бұрын

    your brute force solution is optimal solution for me 🤣🤣

  • @pihus3498
    @pihus3498 Жыл бұрын

    understoood :)

  • @mohammedahtesham7972
    @mohammedahtesham79729 ай бұрын

    understood

  • @bhanusingh4351
    @bhanusingh435110 ай бұрын

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

  • @dreamyme543
    @dreamyme5439 ай бұрын

    Understood🙃

  • @dhamotz4737
    @dhamotz473716 күн бұрын

    can someone explain me the missing number formulae in the brute force as its setting correct for the case which striver has taken but once I am changing the values in the array and trying to find the new missing number I am not getting what I should get, did I get the question in a wrong manner , am I missing any key point of the question ?

  • @her_soulmate
    @her_soulmate6 ай бұрын

    Understand 🎉

  • @PeeyushSharma-pc8fc
    @PeeyushSharma-pc8fc17 күн бұрын

    the optimal solution definitely does not fit in the easy category striver 😂 btw amazing lecture🙏🏻♥

  • @sairohith1363
    @sairohith13639 ай бұрын

    Understood

  • @nayankhuman1043
    @nayankhuman104316 сағат бұрын

    Understood :)

  • @satendra6200
    @satendra620011 ай бұрын

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

  • @user-xp3hf2mb2u
    @user-xp3hf2mb2u4 ай бұрын

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

  • @badrirao6472
    @badrirao6472Ай бұрын

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

  • @rhul0017
    @rhul00179 ай бұрын

    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

  • @shubhamsingh4701
    @shubhamsingh470111 ай бұрын

    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

    10 ай бұрын

    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]

  • @ambaradhikari7425
    @ambaradhikari7425 Жыл бұрын

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

  • @adityakanwar8370

    @adityakanwar8370

    Жыл бұрын

    +1

  • @amrishshah8982
    @amrishshah898210 ай бұрын

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

  • @sujalgupta6100
    @sujalgupta61002 ай бұрын

    GodLike!

  • @chirag.3082
    @chirag.3082Ай бұрын

    woozy of a question

  • @aryansinha1818
    @aryansinha18187 ай бұрын

    Why does it not work on gfg?

  • @vharshita-334_
    @vharshita-334_20 күн бұрын

    where are notes as i m unable to find it in description?

  • @anshulrawat3458
    @anshulrawat34589 ай бұрын

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

  • @VishalGupta-xw2rp
    @VishalGupta-xw2rp9 ай бұрын

    13:10 opposite polarity

  • @souhardayasaha
    @souhardayasaha Жыл бұрын

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