Array - 51: Find the first K missing Positive number
Вставка
- Опубліковано 8 лют 2025
- Source Code:thecodingsimpl...
Solution - 1: Using Sorting
We sort the array
Now start iterating the element: Ignore negative element & as soon as you get positive number, keep monitoring numbers
Whenever you find number mismatching, add numbers in missing list.
Time Complexity: O(nlog(n)), where n is number of values in array
Space Complexity: O(n)
Solution - 2:
We'll start from 0th index & iterate all elements of all array
If arr value is positive & less than equal to array lenegth & arr[i] is not arr[arr[i] - 1], then we need to swap the value, So we get the index value & put this value to it's correct index
If it's not the case, then we move to next index.
Now we traverse array values and if arr value is not equal to index + 1, add index + 1 in missing number list
Time Complexity: O(n)
Space Complexity: O(n)
For more info, please see the video.
CHECK OUT CODING SIMPLIFIED
/ codingsimplified
★☆★ VIEW THE BLOG POST: ★☆★
thecodingsimpli...
I started my UA-cam channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 500+ videos.
★☆★ SUBSCRIBE TO ME ON UA-cam: ★☆★
www.youtube.co...
★☆★ Send us mail at: ★☆★
Email: thecodingsimplified@gmail.com
Hi, have you quiet UA-cam?
Plz continue, will achieve more and thank you so much for this exceptional arrays playlist.
Could you do a leetcode series playlist? I'm sure many will be interested. Leetcode has the most exposure so I believe many will search in youtube for that. Just a suggestion.
Also, you have put out so many good content, you deserve more subscribers.
Thanks Vinay for your feedback. yeah, definitely, help me in it. Share the channel with your friends. Subscribers, likes really gives motivation to keep working on.
That's very nice suggestion. Sure, I'll look into this.
good explanation...keep going...
Thanks Srilam for your feedback. Keep Watching.
Can u explain how its time complexity is O(n)? For 2nd method
Can we use set and store all the element there and check if count is in set or not ..if not then count is missing and else count+=1 and this will go till Len of missing is less than k
Looking good. Thanks for mentioning this solution.