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

  Рет қаралды 120,976

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

Пікірлер: 287
@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
@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 11 ай бұрын
😂😂 this correction helped me so much🤣
@sidharthjain2129
@sidharthjain2129 10 ай бұрын
TBH it did help understand the brute force. Thanks mate.
@rounaq_khandelwal
@rounaq_khandelwal 9 ай бұрын
@@sidharthjain2129 welcome!
@Anony.musk01
@Anony.musk01 Жыл бұрын
correction: 21:18 time complexity should be O(logN)* . Great explanation though
@kirannagulakonda3443
@kirannagulakonda3443 3 ай бұрын
Happy to solve without watching solution , took 4hrs to figure out approach and code it . Yeah happy me🙂
@aryansinha1818
@aryansinha1818 8 ай бұрын
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
@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 9 ай бұрын
you are far better than any of the comedians and so called celebrities . Thanks!
@swayamsharma7614
@swayamsharma7614 23 күн бұрын
in what sense you are comparing ?
@bhaskararya9112
@bhaskararya9112 24 күн бұрын
Awesome explanation man, after some self dry run of few examples, I completely got the concept. Thanks!
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Very beautiful explained. With logic
@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.
@sagarsrivastava1236
@sagarsrivastava1236 4 ай бұрын
Pure Genius Explanation....
@adityamaurya6646
@adityamaurya6646 12 күн бұрын
This guy's just incredibleee!
@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
@abhishekshinde5375
@abhishekshinde5375 20 күн бұрын
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
@harrisjutt5021
@harrisjutt5021 4 күн бұрын
Thank you very much man 💜💜
@user-ti3bd8mp1w
@user-ti3bd8mp1w Жыл бұрын
understood Thank you striver for such an amazing explanation
@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?😐😐
@ipshitatandon5134
@ipshitatandon5134 Ай бұрын
Crazy derivation sir! thank youu for the series
@Josuke217
@Josuke217 10 күн бұрын
Nice question, learned a new concept.
@purushottam108
@purushottam108 Ай бұрын
simple problem koo completed or comopleted problem koo simple, bana koi aap sai sikhai, you are master of dsa🤩🤩🤩🤩❤❤❤❤❤❤❤❤
@girikgarg8
@girikgarg8 14 күн бұрын
Great explanation !
@lostcyrus8578
@lostcyrus8578 Жыл бұрын
Bhaiya you are doing a great work ❤
@rushilvyas9869
@rushilvyas9869 2 ай бұрын
Loved the explanation.
@santhosh7042
@santhosh7042 9 ай бұрын
🤯 didn't thought for the binary search approach
@AnmolGupta-oj4lm
@AnmolGupta-oj4lm 11 ай бұрын
Understood Very Well!
@lokeshnegi5051
@lokeshnegi5051 15 күн бұрын
awesome explanation
@user-or5oz1pk2x
@user-or5oz1pk2x 3 ай бұрын
Thanks a lot Bhaiya . Crystal Clear
@yoMisterWhyte
@yoMisterWhyte 11 ай бұрын
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!
@ayushmanpaul3931
@ayushmanpaul3931 11 ай бұрын
great explanation for low+k.
@ReD4eva94
@ReD4eva94 6 ай бұрын
Brilliant. Thanks!
@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!
@seekingsoul871
@seekingsoul871 18 күн бұрын
Best explanation
@immrhrr
@immrhrr 16 күн бұрын
thankfully I was able to do it by my own. I was able to think this approach myself
@mikedelta658
@mikedelta658 6 ай бұрын
Fantastic. Thank you.
@jaganmohan2198
@jaganmohan2198 3 ай бұрын
nicely explained bro ....thanks
@thedon713
@thedon713 9 ай бұрын
you are the best brother
@playwithlinux
@playwithlinux Ай бұрын
Nice explanation Sir ❤❤❤
@pulkitjain5159
@pulkitjain5159 Жыл бұрын
amazing solution , maja agya
@Gaurav_Tripathi_
@Gaurav_Tripathi_ 8 ай бұрын
Understood Bhaiya ..!!
@NazeerBashaShaik
@NazeerBashaShaik 3 ай бұрын
Understood, thank you.
@_SahilShah
@_SahilShah 17 күн бұрын
letsss goooo!! Understood
@mzia1210
@mzia1210 11 ай бұрын
Helped a lot 🙂🙂😌
@sarangkumarsingh7901
@sarangkumarsingh7901 2 ай бұрын
Awesome Sir......
@YourCodeVerse
@YourCodeVerse 7 ай бұрын
Understood✅🔥🔥
@happyteen7195
@happyteen7195 10 ай бұрын
how beautifully u explained the brute force. top level intuition👑.
@Shunya_Advait
@Shunya_Advait 11 ай бұрын
Understood Sir.
@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 ?
@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
@tridibeshmisra9426
@tridibeshmisra9426 5 ай бұрын
good question .. maza aya karke
@rahulkarki6857
@rahulkarki6857 Жыл бұрын
Totally dependent on you bro ❤
@harshdiwase1941
@harshdiwase1941 4 ай бұрын
I coudln't think this way !! best solution
@md.imrankhan6912
@md.imrankhan6912 10 ай бұрын
Legendary Boss
@culeforever5408
@culeforever5408 9 ай бұрын
understood 😇
@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 9 ай бұрын
we learn here how to implement binary search not only how to solve in this specific problem.
@rishabh_pant
@rishabh_pant 8 ай бұрын
its perfect
@shrinikclub8883
@shrinikclub8883 8 ай бұрын
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-xe2jt5vl5g
@user-xe2jt5vl5g 8 ай бұрын
i knew there was better algo than quick select thanks
@Blissfulvibes3401
@Blissfulvibes3401 Жыл бұрын
thank you so much sir
@Amine-yq7jg
@Amine-yq7jg Жыл бұрын
understood!
@samreenimam8608
@samreenimam8608 6 ай бұрын
bar bar dekho... hazaar bar dekho or practice kro is the key here
@lucario404
@lucario404 Жыл бұрын
@takeUforward maybe you can update the time complexity in description for binary search as O(log2N)
@senseiAree
@senseiAree 9 ай бұрын
Understood ❤
@user-tk2vg5jt3l
@user-tk2vg5jt3l 5 ай бұрын
Thank you Bhaiya
@harshitjaiswal9439
@harshitjaiswal9439 Жыл бұрын
Understooooooood!
@user-is6ky7pp2n
@user-is6ky7pp2n 2 ай бұрын
Understood !! 😎😎
@saketjaiswal9381
@saketjaiswal9381 Жыл бұрын
21:23 error time complexity will be o(log base 2 n) that is o(log n)
@Kaushik846
@Kaushik846 Жыл бұрын
Understood!!
@PeeyushSharma-pc8fc
@PeeyushSharma-pc8fc 28 күн бұрын
the optimal solution definitely does not fit in the easy category striver 😂 btw amazing lecture🙏🏻♥
@dank7044
@dank7044 8 ай бұрын
the brute force solution is beautiful.\
@nayankhuman1043
@nayankhuman1043 11 күн бұрын
Understood :)
@Akhilkumar0024
@Akhilkumar0024 26 күн бұрын
this question scared the living daylights out of me, couldn't solve it even brute force was this really an easy question
@pihus3498
@pihus3498 Жыл бұрын
understoood :)
@her_soulmate
@her_soulmate 6 ай бұрын
Understand 🎉
@ankitgupta7467
@ankitgupta7467 10 ай бұрын
Hey Bhagwan , The BS intuition great
@prakharsahu5778
@prakharsahu5778 Жыл бұрын
when are you planning to complete this series ?
@anonymousits
@anonymousits 3 ай бұрын
understood
@dreamyme543
@dreamyme543 10 ай бұрын
Understood🙃
@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!
@parth2439
@parth2439 5 ай бұрын
Understood
@U2011-n7w
@U2011-n7w 10 ай бұрын
why this question is tagged easy on leetcode
@VivekYadav-uy9ts
@VivekYadav-uy9ts 8 ай бұрын
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!
@AS-gf3ci
@AS-gf3ci Жыл бұрын
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?
@bhanusingh4351
@bhanusingh4351 10 ай бұрын
at 21:17 , time complexity would be O(logn) [ just a human error]
@sujalgupta6100
@sujalgupta6100 2 ай бұрын
GodLike!
@nousad-ali-0
@nousad-ali-0 5 ай бұрын
After Watching 3 time I Finally understand
@dhamotz4737
@dhamotz4737 27 күн бұрын
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
@mySpace940
@mySpace940 9 ай бұрын
how in explanation high
@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
@chirag.3082
@chirag.3082 Ай бұрын
woozy of a question
@amitfritz11
@amitfritz11 Жыл бұрын
I just wonder this is only first video bit difficult to understand both brute force and optimal.
@loveglobe
@loveglobe 9 күн бұрын
jab tak suraj chaand rahega , striver tera naam rahega
@dhamotz4737
@dhamotz4737 27 күн бұрын
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 ?
@akworld2739
@akworld2739 9 ай бұрын
your brute force solution is optimal solution for me 🤣🤣
@VishalGupta-xw2rp
@VishalGupta-xw2rp 10 ай бұрын
13:10 opposite polarity
@ambaradhikari7425
@ambaradhikari7425 Жыл бұрын
Bro by when your whole dsa sheet will be covered, placement coming up?any eta approx?
@adityakanwar8370
@adityakanwar8370 Жыл бұрын
+1
@rohandas6298
@rohandas6298 Жыл бұрын
int missingK(vector < int > vec, int n, int k) { int low=0,high=n-1; while(low
@CodeMode9313
@CodeMode9313 11 ай бұрын
Habibi batai toh mast hai tum ....correction :) @ 21:18 time stamp ie TC be logn
@preritphoenixgupta330
@preritphoenixgupta330 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@pulkitjain5159 okay, thanks bhai
@saketsoni2587
@saketsoni2587 Жыл бұрын
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 Жыл бұрын
@@saketsoni2587 Ok Thanks
@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
@anshulrawat3458
@anshulrawat3458 10 ай бұрын
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..
@Manishgupta200
@Manishgupta200 Жыл бұрын
Optimal code takes much time to understand
@umangkumar2005
@umangkumar2005 5 ай бұрын
int missingK(vector < int > arr, int n, int k) { int low=0; int high=n-1; int mid=low+(high-low)/2; while(low
@badrirao6472
@badrirao6472 2 ай бұрын
why cant we use arr[mid] -- (mid + 1 ) formula to calculate the missing number instead of the formula arr[high ] -- (high + 1) ??
BS-17. Aggressive Cows | Binary Search Hard
26:44
take U forward
Рет қаралды 135 М.
BS-18. Allocate Books or Book Allocation | Hard Binary Search
27:29
take U forward
Рет қаралды 143 М.
НЫСАНА КОНЦЕРТ 2024
2:26:34
Нысана театры
Рет қаралды 1,4 МЛН
Inside Out Babies (Inside Out Animation)
00:21
FASH
Рет қаралды 22 МЛН
Они так быстро убрались!
01:00
Аришнев
Рет қаралды 2 МЛН
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 7 МЛН
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
Doing LeetCode Be Like (Coding Interviews Be Like Pt. 2)
4:41
Nicholas T.
Рет қаралды 754 М.
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 110 М.
5 Math Skills Every Programmer Needs
9:08
Sahil & Sarra
Рет қаралды 1 МЛН
BS-15. Capacity to Ship Packages within D Days
20:36
take U forward
Рет қаралды 82 М.
Count Subarray sum Equals K | Brute - Better -Optimal
24:09
take U forward
Рет қаралды 253 М.
НЫСАНА КОНЦЕРТ 2024
2:26:34
Нысана театры
Рет қаралды 1,4 МЛН