First missing positive

Input: nums = [1,2,0]
Output: 3
Input: nums = [3,4,-1,1]
Output: 2
Input: nums = [7,8,9,11,12]
Output: 1
loop from 1 to length of nums+1:
check if the number is present in the nums array
if number not present in nums :
return number
class Solution(object):
def firstMissingPositive(self, nums):
val=set(nums)
for i in range(1,len(nums)+2):

if i not in val and i>0:
return i
class Solution(object):
def firstMissingPositive(self, nums):
i=0
while(i<len(nums)):
if nums[i]<=0 or nums[i]>len(nums):
i+=1
else:
if nums[i]==i+1:
i+=1
else:
if nums[nums[i]-1]==nums[i]:
i+=1
else:
temp=nums[nums[i]-1]
nums[nums[i]-1]=nums[i]
nums[i]=temp
j=0
while(j<len(nums)):
if(j+1!=nums[j]):
return(j+1)
j+=1
return(len(nums)+1)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.