First Missing Positive | LeetCode 41 | C++, Java, Python | BITWISE operation

preview_player
Показать описание

**** Best Books For Data Structures & Algorithms for Interviews:**********
*****************************************************************************

September LeetCoding Challenge | Problem 30 | First Missing Positive | 30 September,
Facebook Coding Interview question,
google coding interview question,
leetcode,
First Missing Positive,
First Missing Positive c++,
First Missing Positive Java,
First Missing Positive python,
First Missing Positive solution,
41. First Missing Positive,

#CodingInterview #LeetCode #Google #Amazon
Рекомендации по теме
Комментарии
Автор

I'm damn sure no better solution is available than this. Thanks a ton, sir ji! I was about to give up but found your video. Keep continue this series.

husler
Автор

Really loved your explanation among all the other YouTube channels present. I was only halfway through this video and it gave me enough confidence and clarity to solve this question by my own. Keep working on the good stuff 😄

ramanujachanduri
Автор

Nice. I finally get it. Your visuals are perfect.

pqsk
Автор

Hi, Did you stoped making newer videos on Leetcode problems?

shubhamsamdani
Автор

Sir, Excellent video, but sir I did not understand why do we have to do x=abs(arr[i ]) ?? 
As we already put 1 for the negative values In the above loop ? So why we are again checking for positive??

satyammishra-xbhs
Автор

How the heck can you figure this out without seeing the dang solution for O(1) time. Crazy

beaglesnlove
Автор

@Knowledge Center, It will be great if you can add leetcode problem "Word Break"

rishidhawan
Автор

there is an issue in your logic

updated your logic, just checking the input array has 1 or not, in that case return 1 as an output

public int FirstMissingPositive(int[] nums)
{
int smallestPositive = int.MaxValue;
int length = nums.Length;
// Step 1 -
// loop entire array and mark the cells as 1 wherever cell value is <= 0 or value > N
for (int i = 0; i < length; i++)
{
if(nums[i] > 0)
{
smallestPositive = Math.Min(smallestPositive, nums[i]);
}
if (nums[i] <= 0 || nums[i] > length)
{
nums[i] = 1;
}
}

if (smallestPositive != 1)
{
return 1;
}

// Step 2 -
// loop entire array and for each cell value -1 position, update to negetive to mark that value is present in range of 1-N
for (int i = 0; i < length; i++)
{
int val = Math.Abs(nums[i]);
if(nums[val - 1] > 0)
nums[val - 1] *= -1;
}
// Step 3 -
// loop entire array, any value as positive, that position + 1 is the missing element, if not found then N+1 is the missing element(eg- (1, 2, 3, 4, 5)=>O/p-6)
for (int i = 0; i < length; i++)
{
if (nums[i] > 0) return i + 1;
}
return length + 1;
}

joydeeprony
Автор

But it will not work if there has already negative number or 0 in array

giit
Автор

Sir, I write the same code as you, it ran but my program didn't submit

avinashmaurya
Автор

Please do make video on Leetcode Challenge September 29 - Word Break.

shavisharma
Автор

I think the problem really starts after 6 minutes

jax
Автор

not working for input - 7, 8, 9, 11, 12

joydeeprony
Автор

4 for loops 😐...I did it using one while loop and one for

akashyadav
Автор

why are we converting negavtives into 1 .

ss-xhhf
visit shbcf.ru