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

  Рет қаралды 110,193

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

Пікірлер: 262
@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 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!
@Anony.musk01
@Anony.musk01 Жыл бұрын
correction: 21:18 time complexity should be O(logN)* . Great explanation though
@prathamsharma4802
@prathamsharma4802 8 ай бұрын
you are far better than any of the comedians and so called celebrities . Thanks!
@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.
@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
@cinime
@cinime Жыл бұрын
Understood! Super fantastic explanation as always, thank you so so much for your effort!!
@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 Жыл бұрын
💯
@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 25 күн бұрын
how does this works ? where we would write this after when high cross low ?
@sukhii0220
@sukhii0220 25 күн бұрын
@@003_baneshubhamvijay3 how does it working ? it gives 6 if we apply in [4, 7,9] and k=3
@varunpalsingh3822
@varunpalsingh3822 11 ай бұрын
Optimal one is just out of the box kind of thing ❤
@shreyxnsh.14
@shreyxnsh.14 5 ай бұрын
yeah, you can only solve this question in the interview if you have solved it before
@Ayush37262
@Ayush37262 4 ай бұрын
​@@shreyxnsh.14 Thanks bhai, Solve nhi ho rha tha aur easy tag dekh kar depressed feel ho rha tha...
@saswatrath4646
@saswatrath4646 2 ай бұрын
@@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.
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Very beautiful explained. With logic
@kirannagulakonda3443
@kirannagulakonda3443 2 ай бұрын
Happy to solve without watching solution , took 4hrs to figure out approach and code it . Yeah happy me🙂
@bhaskararya9112
@bhaskararya9112 5 сағат бұрын
Awesome explanation man, after some self dry run of few examples, I completely got the concept. Thanks!
@user-ti3bd8mp1w
@user-ti3bd8mp1w 11 ай бұрын
understood Thank you striver for such an amazing explanation
@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 2 ай бұрын
great observation, it will help me memorize this algorithm better, thanks!
@mittal_aaditya
@mittal_aaditya 3 ай бұрын
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!
@sagarsrivastava1236
@sagarsrivastava1236 3 ай бұрын
Pure Genius Explanation....
@lostcyrus8578
@lostcyrus8578 Жыл бұрын
Bhaiya you are doing a great work ❤
@AnmolGupta-oj4lm
@AnmolGupta-oj4lm 11 ай бұрын
Understood Very Well!
@santhosh7042
@santhosh7042 8 ай бұрын
🤯 didn't thought for the binary search approach
@ipshitatandon5134
@ipshitatandon5134 14 күн бұрын
Crazy derivation sir! thank youu for the series
@ayushmanpaul3931
@ayushmanpaul3931 11 ай бұрын
great explanation for low+k.
@purushottam108
@purushottam108 27 күн бұрын
simple problem koo completed or comopleted problem koo simple, bana koi aap sai sikhai, you are master of dsa🤩🤩🤩🤩❤❤❤❤❤❤❤❤
@ReD4eva94
@ReD4eva94 5 ай бұрын
Brilliant. Thanks!
@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.
@user-or5oz1pk2x
@user-or5oz1pk2x 2 ай бұрын
Thanks a lot Bhaiya . Crystal Clear
@rushilvyas9869
@rushilvyas9869 Ай бұрын
Loved the explanation.
@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 25 күн бұрын
this existingdifff is the difference between the range we find out ?
@mikedelta658
@mikedelta658 5 ай бұрын
Fantastic. Thank you.
@lucario404
@lucario404 Жыл бұрын
@takeUforward maybe you can update the time complexity in description for binary search as O(log2N)
@NazeerBashaShaik
@NazeerBashaShaik 2 ай бұрын
Understood, thank you.
@Shunya_Advait
@Shunya_Advait 10 ай бұрын
Understood Sir.
@thedon713
@thedon713 8 ай бұрын
you are the best brother
@jaganmohan2198
@jaganmohan2198 2 ай бұрын
nicely explained bro ....thanks
@user-xe2jt5vl5g
@user-xe2jt5vl5g 7 ай бұрын
i knew there was better algo than quick select thanks
@happyteen7195
@happyteen7195 9 ай бұрын
how beautifully u explained the brute force. top level intuition👑.
@Gaurav_Tripathi_
@Gaurav_Tripathi_ 8 ай бұрын
Understood Bhaiya ..!!
@YourCodeVerse
@YourCodeVerse 6 ай бұрын
Understood✅🔥🔥
@harshdiwase1941
@harshdiwase1941 3 ай бұрын
I coudln't think this way !! best solution
@user-is6ky7pp2n
@user-is6ky7pp2n Ай бұрын
Understood !! 😎😎
@prakharsahu5778
@prakharsahu5778 Жыл бұрын
when are you planning to complete this series ?
@harshitjaiswal9439
@harshitjaiswal9439 11 ай бұрын
Understooooooood!
@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!
@rahulkarki6857
@rahulkarki6857 Жыл бұрын
Totally dependent on you bro ❤
@saketjaiswal9381
@saketjaiswal9381 11 ай бұрын
21:23 error time complexity will be o(log base 2 n) that is o(log n)
@mzia1210
@mzia1210 11 ай бұрын
Helped a lot 🙂🙂😌
@pulkitjain5159
@pulkitjain5159 Жыл бұрын
amazing solution , maja agya
@sayyidzyanashraf
@sayyidzyanashraf 26 күн бұрын
Nice explanation Sir ❤❤❤
@sarangkumarsingh7901
@sarangkumarsingh7901 2 ай бұрын
Awesome Sir......
@md.imrankhan6912
@md.imrankhan6912 10 ай бұрын
Legendary Boss
@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
@Amine-yq7jg
@Amine-yq7jg Жыл бұрын
understood!
@Kaushik846
@Kaushik846 Жыл бұрын
Understood!!
@senseiAree
@senseiAree 9 ай бұрын
Understood ❤
@629simran6
@629simran6 Жыл бұрын
thank you so much sir
@bhanusingh4351
@bhanusingh4351 10 ай бұрын
at 21:17 , time complexity would be O(logn) [ just a human error]
@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?😐😐
@dogwoofwoof8154
@dogwoofwoof8154 6 ай бұрын
after a good brainstorming session it's time to see the video :)
@Ayush37262
@Ayush37262 4 ай бұрын
Did you solved the question before watching solution??
@dogwoofwoof8154
@dogwoofwoof8154 4 ай бұрын
@@Ayush37262 yep
@Ayush37262
@Ayush37262 4 ай бұрын
@@dogwoofwoof8154 You are Batman!
@samreenimam8608
@samreenimam8608 6 ай бұрын
bar bar dekho... hazaar bar dekho or practice kro is the key here
@culeforever5408
@culeforever5408 8 ай бұрын
understood 😇
@Akhilkumar0024
@Akhilkumar0024 2 күн бұрын
this question scared the living daylights out of me, couldn't solve it even brute force was this really an easy question
@tridibeshmisra9426
@tridibeshmisra9426 4 ай бұрын
good question .. maza aya karke
@user-tk2vg5jt3l
@user-tk2vg5jt3l 4 ай бұрын
Thank you Bhaiya
@mySpace940
@mySpace940 9 ай бұрын
how in explanation high
@pihus3498
@pihus3498 Жыл бұрын
understoood :)
@her_soulmate
@her_soulmate 6 ай бұрын
Understand 🎉
@dank7044
@dank7044 7 ай бұрын
the brute force solution is beautiful.\
@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
@nousadali4808
@nousadali4808 4 ай бұрын
After Watching 3 time I Finally understand
@dhamotz4737
@dhamotz4737 3 күн бұрын
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 5 ай бұрын
faxx, how is your dsa prep now btw?
@abhinavabhi3568
@abhinavabhi3568 4 ай бұрын
Understood
@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 9 ай бұрын
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]
@rhul0017
@rhul0017 8 ай бұрын
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 9 ай бұрын
Understood🙃
@anonymousits
@anonymousits 3 ай бұрын
understood
@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..
@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 Ай бұрын
why cant we use arr[mid] -- (mid + 1 ) formula to calculate the missing number instead of the formula arr[high ] -- (high + 1) ??
@user-gf1ew5wd1p
@user-gf1ew5wd1p 11 ай бұрын
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
@ankitgupta7467
@ankitgupta7467 9 ай бұрын
Hey Bhagwan , The BS intuition great
@ShahNawaz-cx3pi
@ShahNawaz-cx3pi 27 күн бұрын
******** 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
@amitfritz11
@amitfritz11 Жыл бұрын
I just wonder this is only first video bit difficult to understand both brute force and optimal.
@ambaradhikari7425
@ambaradhikari7425 Жыл бұрын
Bro by when your whole dsa sheet will be covered, placement coming up?any eta approx?
@adityakanwar8370
@adityakanwar8370 Жыл бұрын
+1
@sujalgupta6100
@sujalgupta6100 2 ай бұрын
GodLike!
@PeeyushSharma-pc8fc
@PeeyushSharma-pc8fc 4 күн бұрын
the optimal solution definitely does not fit in the easy category striver 😂 btw amazing lecture🙏🏻♥
@rethickpavan4264
@rethickpavan4264 Ай бұрын
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 :)
@dhamotz4737
@dhamotz4737 3 күн бұрын
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 ?
@VishalGupta-xw2rp
@VishalGupta-xw2rp 9 ай бұрын
13:10 opposite polarity
@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??
@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....
@sukhii0220
@sukhii0220 25 күн бұрын
arr[mid] - (mid+1) was the formula found out before to find missing nums , then change to arr[high] -(high+1) how ????
@CodeMode9313
@CodeMode9313 10 ай бұрын
Habibi batai toh mast hai tum ....correction :) @ 21:18 time stamp ie TC be logn
@mustafa-zl9ox
@mustafa-zl9ox 11 ай бұрын
class Solution { public: int findKthPositive(vector& arr, int k) { int low = 0 , high = arr.size() -1 ,ans=k+high+1; if(arr[0]>k){ return k; } if(arr[high]-high-1
@Manishgupta200
@Manishgupta200 11 ай бұрын
Optimal code takes much time to understand
@chirag.3082
@chirag.3082 21 күн бұрын
woozy of a question
@ujjwalraj9837
@ujjwalraj9837 11 ай бұрын
bruhh howTf did you thibk of that solution! It was insane.
@vharshita-334_
@vharshita-334_ 7 күн бұрын
where are notes as i m unable to find it in description?
@shivanshumishra0560
@shivanshumishra0560 Жыл бұрын
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 🙏🙏🙏🙏
@aryansinha1818
@aryansinha1818 7 ай бұрын
Why does it not work on gfg?
@rohanmadiratta6421
@rohanmadiratta6421 5 ай бұрын
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 5 ай бұрын
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 :)
BS-17. Aggressive Cows | Binary Search Hard
26:44
take U forward
Рет қаралды 123 М.
Heartwarming: Stranger Saves Puppy from Hot Car #shorts
00:22
Fabiosa Best Lifehacks
Рет қаралды 21 МЛН
孩子多的烦恼?#火影忍者 #家庭 #佐助
00:31
火影忍者一家
Рет қаралды 49 МЛН
🌊Насколько Глубокий Океан ? #shorts
00:42
Two Sum | LeetCode 1 | JavaScript | Easy
13:20
Gordon Zhu
Рет қаралды 8 М.
BS-15. Capacity to Ship Packages within D Days
20:36
take U forward
Рет қаралды 75 М.
❌ Don't Run Behind 500 LEETCODE Problems ❌ Focus on QPCD
8:31
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 615 М.
BS-18. Allocate Books or Book Allocation | Hard Binary Search
27:29
take U forward
Рет қаралды 130 М.
BS 19. Painter's Partition and Split Array - Largest Sum
11:20
take U forward
Рет қаралды 81 М.
First Missing Positive - Leetcode 41 - Python
21:22
NeetCode
Рет қаралды 102 М.