Amazon Coding Interview Question - First Missing Positive (LeetCode)

  Рет қаралды 39,606

AlgosWithMichael

AlgosWithMichael

Күн бұрын

Here is a step by step explanation of a hard array problem asked at Amazon!
Check out my interview prep platform for learning the patterns!
📢 Interview Prep Platform: algoswithmicha...
🎧 Join the community Discord: / discord
💰 Support me on Patreon: / michaelmuinos
🔗Follow me on LinkedIn: / michael-muinos
📂Follow me on Github: github.com/Mic...
In this video, I go over the problem "First Missing Positive". This is a hard problem asked at many tech companies including Amazon, Bloomberg, Google, and many more. This problem would be considered an easy/medium if there wasn't a restriction that we must have a solution with constant extra space.
In the brute force approach, we could add all of the numbers we have in our input to a set. Then, we perform a loop from 1 to N where N is the number of elements we have in our input and check if the number is present in the set. If the number we are on is not in the set, we found our first missing positive. The brute force approach involved a linear time AND space complexity.
Now, in the optimized approach, it takes 3 steps to solve this problem. For step 1, we must convert any negative numbers and numbers greater than N to a 1. This is done to restrict our range of numbers. For step 2, we use the range of numbers as indexes to our array and perform a lookup to swap the sign of the element at the index. This will signify we have seen the positive number without doing any sorting. In the final step, we look for the first non-negative number, if we find one, the index + 1 is the first missing positive; however, if we don't find it, we know the answer will be N + 1.

Пікірлер: 271
@keithnorm82
@keithnorm82 4 жыл бұрын
What a ridiculous problem lol. Can pretty much say I would have never thought of doing it this way. Thanks for the explanation!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
lol yea, the optimized solution is definitely an "outside of the box" approach. Thanks for watching!
@manishpundir9625
@manishpundir9625 4 жыл бұрын
When u started with Brute Force it helped everyone to understand the concept. I have no words how to say thank you. Great work indeed. You earned one more subscriber.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Wow awesome glad to hear!
@JakeyCroth
@JakeyCroth 4 жыл бұрын
Awesome awesome solution. Thank you for it! So refreshing to see it drawn out and explained in full, as opposed to the scores of videos out there that fail to do that.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thanks man! I'm a visual learner myself so a whiteboard explanation makes the most sense haha
@Sumit-yn7dn
@Sumit-yn7dn 4 жыл бұрын
@@AlgosWithMichael bro but if we don't have 1 in array then this code fails as you are swapping every negative number with 1 then this will be marked visited ????
@anurandutta2217
@anurandutta2217 4 жыл бұрын
@@Sumit-yn7dn that's why he is using a variable to check if 1 is present or not during the first traversal.
@pruthvirajk6019
@pruthvirajk6019 4 жыл бұрын
You are awesome ...Basically we are making the numbers inside the array to be in between 1 and n and traversing the array again to mark each and every elements presence by changing the sign at that index which indicates that we have seen the element,Now since we need the smallest number in between 1 and n we are traversing the array again from start....You are really Awesome
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
I appreciate it man! It's a challenging problem for sure haha, thanks for watching!
@vinodrammohan
@vinodrammohan 4 жыл бұрын
The best explanation ever. Thank you so much for explaining slowly with diagrams for each edge case
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad it was helpful!
@PATRICKCHUAD
@PATRICKCHUAD 2 жыл бұрын
thanks for hints that we are only be looking from 1-8. I have seen other video but only now I understand this concept. your explanation is very clear.
@faridaragab_
@faridaragab_ 2 жыл бұрын
The Tone of your voice makes me wanna continue listening forever .. so clear .. Thanks for the awesome video .. ✨
@SwapnilSarkar
@SwapnilSarkar 2 жыл бұрын
Hi man! I don't know if you still read the comments of this video but still want to say that your explanation was great. I am sure that I won't ever forget the approach of this solution. Thank you!
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Glad I could help!
@HungNguyen-tp6vt
@HungNguyen-tp6vt Жыл бұрын
thank you. From start I didn't understand but after watching your video many times. I do understand what you said. It's awesome solution.
@leo8292
@leo8292 4 жыл бұрын
Great solution. I kept attempting to define some flags to create a sort of bound in which I could find the number between the upper and lower values, but this is way more straight forward and elegant. I wonder how you get to a solution like that on your own. Thanks for posting!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Some of the solutions, at least to me, seem too difficult to come up with on your own. In an actual interview you would be getting hints and what not. Thanks for watching!
@hamsalekhavenkatesh3440
@hamsalekhavenkatesh3440 3 жыл бұрын
brilliant solution. I love the way how this problem reduced to cyclic sort
@javaguru7453
@javaguru7453 3 жыл бұрын
kzbin.info check this too
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you!
@prajaktakarandikar3459
@prajaktakarandikar3459 4 жыл бұрын
Hope you are better now. Thanks for the video. Great explanation.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
All better now! Thanks for watching!
@ganeshchowdhary3024
@ganeshchowdhary3024 4 жыл бұрын
Awesome idea handling negative numbers. This should be present in the leetcode solution.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Agreed, thanks for watching!
@amandapanda3750
@amandapanda3750 Жыл бұрын
This is such a silly problem, Wouldn't have thought of this solution. Thanks a lot for your explanation.
@AlgosWithMichael
@AlgosWithMichael Жыл бұрын
lol yea it is a ridiculous question
@vibhoragarwal5425
@vibhoragarwal5425 4 жыл бұрын
Just awesome extremely clear explanation not less than any professional keep making videos it helps us lot thanks man
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thanks, will do!
@AM-nv4ol
@AM-nv4ol 2 жыл бұрын
you're a godsend man. amazing explanation throughout. thank you.
@aaryanbudhirajamusic6874
@aaryanbudhirajamusic6874 4 жыл бұрын
Thank you so much! Amazing Explanation! Subscribed man! Initially, I thought it was an easy problem lol but then I saw your video and saw it was a Hard one, felt less guilty !
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Haha yea its a tough problem to get down to constant space. Thanks for watching, commenting, and subscribing!
@avdeshpatel8631
@avdeshpatel8631 3 жыл бұрын
after seeing the video problem looks easy, great explanation on whiteboard !!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you!
@gauravbhattacharjee4379
@gauravbhattacharjee4379 3 жыл бұрын
Michael, I found gold again! This one
@javaguru7453
@javaguru7453 3 жыл бұрын
kzbin.info check this too
@louistannudin2486
@louistannudin2486 3 жыл бұрын
This solution was better than the leetcode solution article
@ashishdukare1313
@ashishdukare1313 4 жыл бұрын
Thank you so much for such an easy explanation. This just saved my day :). Keep making more such videos.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Anytime! More videos coming every week!
@rohanasokan7338
@rohanasokan7338 3 жыл бұрын
Damn! Saved your day? What really happened to your day? xD
@laugh_d_tale
@laugh_d_tale 4 жыл бұрын
that was the simplest solution i watched . Thank you!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad it helped!
@dips2113
@dips2113 4 жыл бұрын
This was super helpful and was explained very well. Thank you!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
I'm so glad you found it helpful, more videos to come!
@biswamohandwari6460
@biswamohandwari6460 4 жыл бұрын
Man your explanation is brilliant. Awesome!!! Thanks, dude.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad it helped!
@bbbo85
@bbbo85 3 жыл бұрын
that's so clever but shouldn't negatives and elem > n just be some number greater than n? for example 8 because if there weren't any 1s, you would've marked 1 as seen
@vincenttan6303
@vincenttan6303 4 жыл бұрын
this is better than the provided solution in LC which I couldn't understand.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thanks, that was my goal!
@capoditutticapos19
@capoditutticapos19 3 жыл бұрын
Very clear explanation man. This question is crazy though. Ns how a person would get the 'a-ha moment' to think of these steps, especially under pressure of an interview. Maybe i can't step inside the head of a genius lol.
@dmsohel1335
@dmsohel1335 4 жыл бұрын
this was the best solution i have ever seen
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you so much!
@sanjayizardar2263
@sanjayizardar2263 4 жыл бұрын
Great solution.. please keep posting hard problems like this
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad you liked it! Thanks for watching.
@vishalpapnai5461
@vishalpapnai5461 4 жыл бұрын
great explanation Michael. you saved me a lot of time.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad I could help :)
@AbhishekChauhan-df3vb
@AbhishekChauhan-df3vb 4 жыл бұрын
oh wow! I was thinking of so many other ways! shit, I knew this topic of absolute(). Really great explanation, thought me to think how to use techniques that I already know :) thanks a lot man! great video
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Yea, it very tricky haha, thanks for watching!
@aviralpandey4096
@aviralpandey4096 4 жыл бұрын
You made it look so easy. Thanks a lot.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad it helped!
@陈瀚龙
@陈瀚龙 4 жыл бұрын
Great problem, and great explanation. Thanks!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you very much!
@gurpartapsingh1693
@gurpartapsingh1693 4 жыл бұрын
Beautifully explained.. keep it up brother
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you man!
@yitingg7942
@yitingg7942 4 жыл бұрын
How can you explain it so well each time Michael? I am so amazed and really appreciate you!! One follow up, I also did 268. Missing Number and tried to use the same logic here, but how come I just couldn't get it right? Why is that the logic applies here but doesn't apply to an easy problem ?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you so much! I believe the problems are different in the sense of what values are allowed inside of the arrays.
@yitingg7942
@yitingg7942 4 жыл бұрын
@@AlgosWithMichael Yes you are absolutely right! Thank you for your reply!!
@heisenberg1844
@heisenberg1844 4 жыл бұрын
About changing out-of-range numbers to 1, what if 1 is not present in the original array? In that case you'd be adding the number 1 to the array, and hence you won't get 1 as the first missing positive number.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
I do a check if 1 is not present before getting to the main part of the algorithm. So, `if (containsOne == 0) return 1`
@heisenberg1844
@heisenberg1844 4 жыл бұрын
@@AlgosWithMichael Loud and clear. Thanks.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Nice, glad it helped!
@indrianids
@indrianids 2 жыл бұрын
Thanks, it's awesome solution!! have think about this case about 2 days haha
@UtkarshShrivastava6009
@UtkarshShrivastava6009 4 жыл бұрын
After watching the first 10 mins, this problem became an easy one.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Awesome, glad to hear!
@raveenadsouza2095
@raveenadsouza2095 4 жыл бұрын
Quick question, for the brute force, why do we add it to a set and then check? Could we not just check if if i (from 0 -> n) is in the list provided? Is it because finding it in a set rather than a list is O(1) rather than O(n)?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
The list is not sorted, so I don't think we can check if i (from 0 -> n) is in the list on its own.
@raveenadsouza2095
@raveenadsouza2095 4 жыл бұрын
@@AlgosWithMichael Oh I meant specifically for python there’s the “in” keyword to find a value in a list so doing for i in range(1, len(nums)+1): if i not in nums: return i
@paras203
@paras203 4 жыл бұрын
Thanks Michael for the explanation. Just one doubt, if we are making everything greater than or all negative numbers to 1. Now if array is 2,3,8 it gets converted to 2,3,1 and then we check indexing and it will fail and answer will be coming as 4 but it should be 1. Please correct me if i understood it wrong.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
There is a check if there is a 1 in the array before we start changing the values. So if we do NOT find a 1, then we know our answer is 1. It is an edge that we have to consider. 16:11 is where the edge case is handled. Thank you for commenting and watching, I appreciate it!
@paras203
@paras203 4 жыл бұрын
@@AlgosWithMichael Got it Michael. Around 13 minutes,, coding started. So paused the video and was analysing the algorithm and found it. Anyways, Keep up the good work. Thanks. :)
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Anytime!
@anurandutta2217
@anurandutta2217 4 жыл бұрын
Great explanation! Immediately subscribed after watching this video. Btw are you a student or a working professional?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
I appreciate that! I currently work full-time as a Software Engineer.
@anurandutta2217
@anurandutta2217 4 жыл бұрын
@@AlgosWithMichael Your videos are very helpful! Keep up the good work 👍 May I know which company are you currently working for?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
I work for a mid-sized company in LA, not a FANG or anything like that!
@fatimamaj7964
@fatimamaj7964 2 жыл бұрын
Thanks for your great explanation. I am just not sure why when I try the same way for solving the problem with 'python' I got this result from LeetCode: Runtime: 1314 ms, faster than 10.62% of Python online submissions for First Missing Positive. Memory Usage: 52.1 MB, less than 57.17% of Python online submissions for First Missing Positive. which there is high difference with your result
@CronusVelox
@CronusVelox 2 жыл бұрын
JAVA is faster than Python. Also, internet speed can skew your runtime I'm leetcode. I wouldn't worry about it 👌
@bekjanomirzak224
@bekjanomirzak224 3 жыл бұрын
Thank you this great explanation.!!!!!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thanks
@parmanirahulpawan27
@parmanirahulpawan27 4 жыл бұрын
Thank you so much. You explained it so well. Super helpful!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
You're very welcome!
@saulgoodman6710
@saulgoodman6710 Жыл бұрын
can you tell us how does one need to think of the optimized solution? by practice? or any helpful resources that we need to study for these kind of stuff?
@Er0Shara
@Er0Shara 4 жыл бұрын
awesome explanation, keep going!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thanks, will do!
@connect2krish
@connect2krish 4 жыл бұрын
Great explanation! Very helpful. Thanks!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you!
@ShashwataSaha
@ShashwataSaha 3 жыл бұрын
from heapq import heapify, heappop class Solution: def firstMissingPositive(self, nums: List[int]) -> int: nums=set(nums) nums=list(nums) heapify(nums) cnt=0 while len(nums)>0: temp=heappop(nums) if temp
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Yea, it is a good solution, but I would imagine in an interview they would want the more optimized approach
@luciferrex9613
@luciferrex9613 4 жыл бұрын
Hi thanks your videos are very nicely explained you got a lifetime subscriber for you .. please keep the good work
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you so much, that means alot!
@alammahtab08
@alammahtab08 4 жыл бұрын
👍👍 I had to go over this twice to make it stick in my head. Below is the commented code, in case anyone wants to try out. class Solution { public int firstMissingPositive(int[] nums) { if(nums == null || nums.length == 0) return 1; int n = nums.length; boolean containsOne = false; // Step 1 : Sets the containsOne flag to true if input array contains 1 , // also sets all the negative numbers, zero and number's greater than n, to 1 // Make sure you don't mess up the order of statements for(int i = 0; i < n; i++) { if(nums[i] == 1) containsOne = true; else if(nums[i] n) nums[i] = 1; } // if array doesn't contains 1, the first missing positive will be 1 if(!containsOne) return 1; // Step 2 : This step marks the number we have seen, by setting its index // position to a negative number // e.g. if nums[i] = 7, since 7 should come at index 6 (that's why - 1, array indexes starts with 0) // we mark nums[6] = nums[6] * -1 (if nums[6] was not already negative) for(int i = 0; i < n; i++) { int index = Math.abs(nums[i]) - 1; // if its already negative, we don't do anything if(nums[index] > 0) nums[index] *= -1; } // Step 3 : Finally, look for a non negative number in the array // If we find an index i, such that nums[i] is positive, that means // we didn't see the number which should come at this index i // the number that should come at index i should be i+1, so we return i+1 for(int i = 0; i < n; i++) { if(nums[i] > 0) return i + 1; } // If we reach here it means, original input array had all the positive numbers // from 1 to n, so the first missing positive number will be n + 1 return n + 1; } } Tip (Catch bugs in implementation) : Run your code through some examples : [1, 2, 3], [1, 2, 2], [1, 3, 3], [2, 2, 2], [4, 5, 1] github.com/eMahtab/first-missing-positive
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Yea, its a very complicated solution, definitely not intuitive haha
@0anant0
@0anant0 4 жыл бұрын
thanks for the helpful comments in your code -- the step 3 comments helped me understand why we are doing this!
@javeedbasha6088
@javeedbasha6088 4 жыл бұрын
Great explanation. Please do more leetcode hard problems.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you!
@warnercooler4488
@warnercooler4488 3 жыл бұрын
So well explained! Thank you so much!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Glad you enjoyed it!
@Haibrayn42
@Haibrayn42 27 күн бұрын
Why not swapping the numbers to their correct indices instead of looping and modifying the out of bounds to 1? That way in the first loop you would the array in the correct order. In a second loop you can just start with missing=1 and increment it every time you find a number in the correct position, and just break if found a number in the wrong position and return the missing var
@psn999100
@psn999100 4 жыл бұрын
That was an amazing explanation. Thanks
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
No problem, thank you for watching!
@JoffreyB
@JoffreyB 2 жыл бұрын
so what if we get numbers like [7, 8, 9], then we replace whatever is greater than n or less than 1 is 1, we get [1, 1, 1], than we use "seen" step, we get [-1, 1, 1], solution would be first non-negative number's index + 1, i. e. = 2, which is not true, should be 1. What gives? Where did I get it wrong?
@VishalSharma-hq5ti
@VishalSharma-hq5ti 4 жыл бұрын
Amazing explanation bro.. keep it up!!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
I appreciate it, thank you!
@code7434
@code7434 4 жыл бұрын
Super awesome ,very tricky problem
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Yea it definitely is!
@3dhakim
@3dhakim 4 жыл бұрын
This is very helpful! can you help on this Michael Muinos Write a function: int solution(int A[], int N); that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5. Given A = [1, 2, 3], the function should return 4. Given A = [−1, −3], the function should return 1. Write an efficient algorithm for the following assumptions: N is an integer within the range [1..100,000]; each element of array A is an integer within the range [−1,000,000..1,000,000].
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
if it is a problem on leetcode, post the problem and ill add it to my list
@sergten
@sergten 3 жыл бұрын
Nice trick. Probably applicable to other problems.
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Yep, this trick is definitely seen in a lot of array restricted problems.
@lokeshkumarrathinavel6566
@lokeshkumarrathinavel6566 3 жыл бұрын
good explanation. you opened my eye. thanks Man.
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Glad to hear it!
@KanagaveluSugumar
@KanagaveluSugumar 2 жыл бұрын
will it work with [2, 3, 4] where 4 will be swapped to 1 then [2, 3, 1] and all are negative, and the result 1 will not be found? instead 4 will be found? The problem is; it is not sure 1 as an existing value or added as replacement for edge case (negative or greater than size of array)
@geodude9395
@geodude9395 2 жыл бұрын
God Bless You. Because Jesus Christ, Finally someone explains it in English.
@starc701
@starc701 4 жыл бұрын
I dont understood why we cannot write step to else if condition with (Nums[i] n) shouldn't we eleminate zero also. Since we want range 1 to n.. Why then we are only excluding negative and greater than n. You did corrected that at end... I saw it.. Updated comment.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Haha yeah I messed up
@starc701
@starc701 4 жыл бұрын
@@AlgosWithMichael it was very helpful bro... Keep up the excellent work.
@deepanshuchawala216
@deepanshuchawala216 4 жыл бұрын
Bro, Thanks a lot . You saved us.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Haha awesome, glad it helped!
@nikhilbhujbal8927
@nikhilbhujbal8927 4 жыл бұрын
Great Explanation bro .
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you 🙂
@bhavyabansal1143
@bhavyabansal1143 2 жыл бұрын
if all numbers are negative lets say -1,-2,-3, then first missing positive is 1 not array.length + 1.. how come this solution works then?
@dhavalkolapkar1772
@dhavalkolapkar1772 3 жыл бұрын
Awesome explanation. I have a question, why use a set? cant we just do Array.contains() ?
@cbjueueiwyru7472
@cbjueueiwyru7472 3 жыл бұрын
Set is o(1) lookup. Array.contains is o(n)
@theuser7288
@theuser7288 4 жыл бұрын
Explained very well.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you!
@arpit_singh10
@arpit_singh10 4 жыл бұрын
Great explanation bro
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thanks man!
@aishwaryavenkat3029
@aishwaryavenkat3029 3 жыл бұрын
Similar to counting sort where we use cur value as index and modify value in that index. Is there a generic name for that technique ? How do one comes up with such technique?
@javaguru7453
@javaguru7453 3 жыл бұрын
kzbin.info check this too
@madhuravp
@madhuravp 3 жыл бұрын
Amazing explanation!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thanks!
@javaguru7453
@javaguru7453 3 жыл бұрын
kzbin.info check this too
@nidhimittal9521
@nidhimittal9521 2 жыл бұрын
Thanks for the amazing explanation! I have one question if(nums[i] == 1) containsOne = true; else if(nums[i] n){ nums[i] = 1; Why the second condition is inside else if? If we have 1 in an array does it mean we don't want to update number less than equal to 0 and greater than n to 1?
@khateebanwer7466
@khateebanwer7466 2 жыл бұрын
He combined the first step and the initial step to deal with an edge case:- 1) we set all values =1 that aren't needed to proceed to the second step. 2) We also use a flag variable to see if it encounters 1 in the array in the first pass itself and if it doesn't enocunter a 1 in first pass i.e that smallest missing positive value would be equal to 1 3) after first pass / first execution phase , we would've checked for both the edge case (i.e 1 exists in the array or not) and also converted the useless values to one to start the next phase thereby skipping one more loop that would run till nums.length for this phase if this edge case was dealt seperately.
@ShashwataSaha
@ShashwataSaha 3 жыл бұрын
How am I sure there will be always 1 in my test case?
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
There won't necessarily always be a 1 in your test case, but it is an edge case you must handle if there is
@Nickel80
@Nickel80 4 жыл бұрын
Thanks for the great explanation. Is there a name for this technique?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Not really. The solution to this problem is really unique haha. Thanks for watching!
@javaguru7453
@javaguru7453 3 жыл бұрын
kzbin.info check this too
@prasanthkumar6393
@prasanthkumar6393 Жыл бұрын
Good solution ❤
@jitendrasinghbisht2289
@jitendrasinghbisht2289 3 жыл бұрын
This is awesome. Thanks Man
@javaguru7453
@javaguru7453 3 жыл бұрын
kzbin.info check this too
@amu281
@amu281 3 жыл бұрын
Hi MIchael, I have a doubt if you could clear. How it will behave for [-5, -4, -3] .
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
I believe 1 would be the returned answer
@codingepocs2410
@codingepocs2410 3 жыл бұрын
best explanation thank you
@javaguru7453
@javaguru7453 3 жыл бұрын
kzbin.info check this too
@a-33-akshaymishra5
@a-33-akshaymishra5 4 жыл бұрын
very well explained!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad to hear, thank you!
@cbjueueiwyru7472
@cbjueueiwyru7472 3 жыл бұрын
What if 1 was the missing positive number. Wouldn't switch those to 1 mask that?
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
I do a check if the missing number is 1, it is an extra edge that is definitely a gotcha!
@AmitSharma-wx1wj
@AmitSharma-wx1wj 4 жыл бұрын
what happens for [0,1,2] -> 0 -1 = -1 wil come right?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
We do an absolute value check of the index so no index out of bounds. Also, the 0 will get changed to a 1 before the negation step!
@AmitSharma-wx1wj
@AmitSharma-wx1wj 4 жыл бұрын
@@AlgosWithMichael For 0, it is not set it to 1 -- in first loop. So in second loop, int index = abs(0) - 1 = -1 will give out of bounds man.. let me know if I m missing something
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
If you go to 19:27, I fix that mistake of not doing `
@AmitSharma-wx1wj
@AmitSharma-wx1wj 4 жыл бұрын
I was trying to do it of my own after watching your approach which confused me.. you are right ! Thanks for the video !
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Gotcha, well thank you commenting and watching!
@anshuldogra7607
@anshuldogra7607 4 жыл бұрын
What if the array is like A[] = {28,10,18,99,70} ?? In this case your logic that the missing number will lie between 1 to n+1 doesn't hold ! Am I missing something here ??
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
The first missing positive in the example you provided is 1. You know your missing number will always be in the range of however many elements you have. The +1 comes from an example such as this [1, 2, 3], the first missing number would be n + 1 where n = 3, 3 + 1 = 4.
@anshuldogra7607
@anshuldogra7607 4 жыл бұрын
@@AlgosWithMichael I had understood the problem incorrectly. I thought it has to be the next missing positive which in this case would have been 11. Thanks for the explanation. You've got yourself a new subscriber :)
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thanks so much!
@carliux8888888
@carliux8888888 3 жыл бұрын
I would never find this solution during an interview... Best I could do is O(N) time and space complexity by using a hash table...
@SC2BuildOrder
@SC2BuildOrder 4 жыл бұрын
Why do you check the absolute value in step 2? You already turn all negatives into positive 1s in step 1...?
@SC2BuildOrder
@SC2BuildOrder 4 жыл бұрын
nvm, im dum
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Haha naw it's a confusing solution
@tejasghone5118
@tejasghone5118 4 жыл бұрын
That was smart ; ) . Thank you!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
No problem!
@talwaar007
@talwaar007 Жыл бұрын
There is no way somebody would work this out in a one hour interview. I figured out a slightly sub-optimal solution which relied on first sorting the input array in about an hour but there is no way in hell I would have figured this out (in fact I couldn't figure it out regardless of time limit).
@code7434
@code7434 4 жыл бұрын
we can replace out of bound numbers by INT_MAX? then during second iteration if abs(nums[i]) is INT_MAX just continue?
@VishalSharma-hq5ti
@VishalSharma-hq5ti 4 жыл бұрын
Agreed I think replacing by 1 might cause an error? I think we can replace out of range numbers by any number in the range(1...n+1), provided that number is present in array or else we can choose an arbitrary number(e.g. INT_MAX)
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Yea I believe you could. However, I could see an edge case where a number in your array is INT_MAX. Thanks for watching!
@mohdaman7958
@mohdaman7958 3 ай бұрын
Hi thanks. But you explained this in a very complex way. We are all three friends unable to understand. Could you please make it a little bit simple.
@veliea5160
@veliea5160 3 жыл бұрын
Can you please solve leetcode 84- Largest Rectangle in Histogram. It is a hard problem and there is not good explanation on youtube
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Yea sure 👍
@veliea5160
@veliea5160 3 жыл бұрын
@@AlgosWithMichael thank you
@Tarun_Kumar_bhu
@Tarun_Kumar_bhu Жыл бұрын
[2, 2, 2, 2, 2] is it going to work?
@HARSHITKUMAR-wj4ex
@HARSHITKUMAR-wj4ex 4 жыл бұрын
very helpful. Thanks!!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
You're welcome! Thanks so much for watching!
@oxanasf6369
@oxanasf6369 4 жыл бұрын
So far it's the most clear explanation even though I work with python. ps Still would be great to have it on python.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad it helped!
@tirthjayswal9895
@tirthjayswal9895 4 жыл бұрын
Awsome Dude ..best
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
No problem man!
@rak590
@rak590 4 жыл бұрын
Please post more videos :)
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
More every week!
@shaileshhegde9205
@shaileshhegde9205 4 жыл бұрын
That was awesome!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad you liked it!
@javaguru7453
@javaguru7453 3 жыл бұрын
kzbin.info check this too
@sandeepkumawat4982
@sandeepkumawat4982 4 жыл бұрын
best explanation ...
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
I appreciate that, thank you
@psychoticgamer6853
@psychoticgamer6853 3 жыл бұрын
Why I'm hearing *Intelligence beat*
@interviewprep6827
@interviewprep6827 3 жыл бұрын
can you post a link to the code please??
@codestorywithMIK
@codestorywithMIK 4 жыл бұрын
Amazing
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thanks!
@mrinalmathur8185
@mrinalmathur8185 4 жыл бұрын
Why are we doing index+1 to search for the number value of index? in 11:29
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Because index is 0 based, thus we must offset it. Thank you for watching!
Amazon Coding Interview Question - Min Stack (LeetCode)
8:34
AlgosWithMichael
Рет қаралды 7 М.
Text Justification Algorithm (LeetCode)
30:45
AlgosWithMichael
Рет қаралды 32 М.
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33
Farmer narrowly escapes tiger attack
00:20
CTV News
Рет қаралды 11 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 30 МЛН
Google Coding Question - Making a Large Island (Hard)
25:11
AlgosWithMichael
Рет қаралды 16 М.
Bucket Sort Interview Question - Min Time Difference (Amazon)
13:52
AlgosWithMichael
Рет қаралды 4,8 М.
First Missing Positive - Leetcode 41 - Python
21:22
NeetCode
Рет қаралды 113 М.
Amazon Coding Interview Question - Recursive Staircase Problem
20:01
Google Coding Interview With A Facebook Software Engineer
49:59
Clément Mihailescu
Рет қаралды 946 М.
Decode String | FAANG Coding Question | Stack
17:03
AlgosWithMichael
Рет қаралды 11 М.
FAANG Coding Interview Question - Container With Most Water (LeetCode)
13:30
Longest Increasing Path in a Matrix (DFS + Memoization)
18:47
AlgosWithMichael
Рет қаралды 19 М.
Microsoft Coding Interview Question - Rotate Image
17:21
AlgosWithMichael
Рет қаралды 9 М.
Google Coding Interview With A Competitive Programmer
54:17
Clément Mihailescu
Рет қаралды 2,5 МЛН