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

  Рет қаралды 115,666

take U forward

take U forward

Жыл бұрын

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
@aryansinha1818 7 ай бұрын
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
@karanhaldar755 3 ай бұрын
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
@kirannagulakonda3443 2 ай бұрын
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 < vec[low]) return k; if (k > vec[high] - n) return k + n;
@Dev-x3b
@Dev-x3b Жыл бұрын
💯
@prathamsharma4802
@prathamsharma4802 8 ай бұрын
you are far better than any of the comedians and so called celebrities . Thanks!
@swayamsharma7614
@swayamsharma7614 11 күн бұрын
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
@bhaskararya9112 13 күн бұрын
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
@abhishekshinde5375 9 күн бұрын
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 < k 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
@santhosh7042 8 ай бұрын
🤯 didn't thought for the binary search approach
@ipshitatandon5134
@ipshitatandon5134 27 күн бұрын
Crazy derivation sir! thank youu for the series
@sagarsrivastava1236
@sagarsrivastava1236 4 ай бұрын
Pure Genius Explanation....
@girikgarg8
@girikgarg8 3 күн бұрын
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 < 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 Жыл бұрын
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
@ayushmanpaul3931 11 ай бұрын
great explanation for low+k.
@adityamaurya6646
@adityamaurya6646 16 сағат бұрын
This guy's just incredibleee!
@rushilvyas9869
@rushilvyas9869 2 ай бұрын
Loved the explanation.
@happyteen7195
@happyteen7195 9 ай бұрын
how beautifully u explained the brute force. top level intuition👑.
@immrhrr
@immrhrr 5 күн бұрын
thankfully I was able to do it by my own. I was able to think this approach myself
@yoMisterWhyte
@yoMisterWhyte 10 ай бұрын
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-oj4lm 11 ай бұрын
Understood Very Well!
@user-or5oz1pk2x
@user-or5oz1pk2x 2 ай бұрын
Thanks a lot Bhaiya . Crystal Clear
@jaganmohan2198
@jaganmohan2198 3 ай бұрын
nicely explained bro ....thanks
@dipanshuraj7868
@dipanshuraj7868 2 ай бұрын
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?😐😐
@ReD4eva94
@ReD4eva94 6 ай бұрын
Brilliant. Thanks!
@lucario404
@lucario404 Жыл бұрын
@takeUforward maybe you can update the time complexity in description for binary search as O(log2N)
@mittal_aaditya
@mittal_aaditya 4 ай бұрын
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
@lokeshnegi5051 4 күн бұрын
awesome explanation
@samreenimam8608
@samreenimam8608 6 ай бұрын
bar bar dekho... hazaar bar dekho or practice kro is the key here
@_SahilShah
@_SahilShah 5 күн бұрын
letsss goooo!! Understood
@mikedelta658
@mikedelta658 5 ай бұрын
Fantastic. Thank you.
@thedon713
@thedon713 8 ай бұрын
you are the best brother
@user-xe2jt5vl5g
@user-xe2jt5vl5g 8 ай бұрын
i knew there was better algo than quick select thanks
@sayyidzyanashraf
@sayyidzyanashraf Ай бұрын
Nice explanation Sir ❤❤❤
@rahulkarki6857
@rahulkarki6857 Жыл бұрын
Totally dependent on you bro ❤
@NazeerBashaShaik
@NazeerBashaShaik 3 ай бұрын
Understood, thank you.
@Shunya_Advait
@Shunya_Advait 11 ай бұрын
Understood Sir.
@tridibeshmisra9426
@tridibeshmisra9426 5 ай бұрын
good question .. maza aya karke
@harshdiwase1941
@harshdiwase1941 3 ай бұрын
I coudln't think this way !! best solution
@mzia1210
@mzia1210 11 ай бұрын
Helped a lot 🙂🙂😌
@seekingsoul871
@seekingsoul871 7 күн бұрын
Best explanation
@prakharsahu5778
@prakharsahu5778 Жыл бұрын
when are you planning to complete this series ?
@YourCodeVerse
@YourCodeVerse 7 ай бұрын
Understood✅🔥🔥
@sarangkumarsingh7901
@sarangkumarsingh7901 2 ай бұрын
Awesome Sir......
@culeforever5408
@culeforever5408 8 ай бұрын
understood 😇
@saketjaiswal9381
@saketjaiswal9381 11 ай бұрын
21:23 error time complexity will be o(log base 2 n) that is o(log n)
@md.imrankhan6912
@md.imrankhan6912 10 ай бұрын
Legendary Boss
@harshitjaiswal9439
@harshitjaiswal9439 11 ай бұрын
Understooooooood!
@dank7044
@dank7044 7 ай бұрын
the brute force solution is beautiful.\
@dogwoofwoof8154
@dogwoofwoof8154 6 ай бұрын
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
@shamanthhegde2820 4 ай бұрын
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
@sukhii0220
@sukhii0220 Ай бұрын
this existingdifff is the difference between the range we find out ?
@U2011-n7w
@U2011-n7w 9 ай бұрын
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!
@mySpace940
@mySpace940 9 ай бұрын
how in explanation high
@senseiAree
@senseiAree 9 ай бұрын
Understood ❤
@user-is6ky7pp2n
@user-is6ky7pp2n Ай бұрын
Understood !! 😎😎
@ankitgupta7467
@ankitgupta7467 10 ай бұрын
Hey Bhagwan , The BS intuition great
@user-tk2vg5jt3l
@user-tk2vg5jt3l 5 ай бұрын
Thank you Bhaiya
@K_EN_VisheshSaini
@K_EN_VisheshSaini 9 ай бұрын
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 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-0 4 ай бұрын
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
@AS-gf3ci
@AS-gf3ci 11 ай бұрын
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?
@amitfritz11
@amitfritz11 Жыл бұрын
I just wonder this is only first video bit difficult to understand both brute force and optimal.
@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
@pihus3498
@pihus3498 Жыл бұрын
understoood :)
@mohammedahtesham7972
@mohammedahtesham7972 9 ай бұрын
understood
@bhanusingh4351
@bhanusingh4351 10 ай бұрын
at 21:17 , time complexity would be O(logn) [ just a human error]
@dreamyme543
@dreamyme543 9 ай бұрын
Understood🙃
@dhamotz4737
@dhamotz4737 16 күн бұрын
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_soulmate 6 ай бұрын
Understand 🎉
@PeeyushSharma-pc8fc
@PeeyushSharma-pc8fc 17 күн бұрын
the optimal solution definitely does not fit in the easy category striver 😂 btw amazing lecture🙏🏻♥
@sairohith1363
@sairohith1363 9 ай бұрын
Understood
@satendra6200
@satendra6200 11 ай бұрын
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-xp3hf2mb2u 4 ай бұрын
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??
@Akhilkumar0024
@Akhilkumar0024 15 күн бұрын
this question scared the living daylights out of me, couldn't solve it even brute force was this really an easy question
@nayankhuman1043
@nayankhuman1043 7 сағат бұрын
Understood :)
@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
@rhul0017 9 ай бұрын
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
@shubhamsingh4701 11 ай бұрын
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
@amrishshah8982 10 ай бұрын
For brute force this case 2,5,6,7,9 and k=5 will fail.. correct me..
@sujalgupta6100
@sujalgupta6100 2 ай бұрын
GodLike!
@akworld2739
@akworld2739 8 ай бұрын
your brute force solution is optimal solution for me 🤣🤣
@chirag.3082
@chirag.3082 Ай бұрын
woozy of a question
@aryansinha1818
@aryansinha1818 7 ай бұрын
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
@anshulrawat3458 9 ай бұрын
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-xw2rp 9 ай бұрын
13:10 opposite polarity
@souhardayasaha
@souhardayasaha Жыл бұрын
bhaiya iska link mein galat question ka code hai aur sheet mein link bhi nahi hai
BS-17. Aggressive Cows | Binary Search Hard
26:44
take U forward
Рет қаралды 129 М.
Looks realistic #tiktok
00:22
Анастасия Тарасова
Рет қаралды 105 МЛН
New model rc bird unboxing and testing
00:10
Ruhul Shorts
Рет қаралды 24 МЛН
HAPPY BIRTHDAY @mozabrick 🎉 #cat #funny
00:36
SOFIADELMONSTRO
Рет қаралды 17 МЛН
НРАВИТСЯ ЭТОТ ФОРМАТ??
00:37
МЯТНАЯ ФАНТА
Рет қаралды 1,9 МЛН
Arithmetic Progression | #shorts
1:00
Galaxy of Maths
Рет қаралды 298 М.
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
VICKY KAUSHAL REACTS TO VICKY KAUSHAL MEMES ft. VICKY KAUSHAL
26:42
Tanmay Bhat
Рет қаралды 5 МЛН
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
Roblox Had A Crazy Update Yesterday...
9:23
Chaseroony
Рет қаралды 414 М.
❌ Don't Run Behind 500 LEETCODE Problems ❌ Focus on QPCD
8:31
Asking GOOGLE & AMAZON 🚀 Engineers how much money do they make? 🤔
15:42
Doing LeetCode Be Like (Coding Interviews Be Like Pt. 2)
4:41
Nicholas T.
Рет қаралды 751 М.
Looks realistic #tiktok
00:22
Анастасия Тарасова
Рет қаралды 105 МЛН