forked from HemangTheHuman/hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirst Missing Positive.cpp
More file actions
37 lines (33 loc) · 1.07 KB
/
First Missing Positive.cpp
File metadata and controls
37 lines (33 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/** First Missing Positive
Given an unsorted integer array nums, return the smallest missing positive integer in O(n) time and uses constant extra space .
**/
#include <iostream>
using namespace std;
// swap function
static void swap(int arr[],int i,int correct) {
int temp=arr[i];
arr[i]=arr[correct];
arr[correct]=temp;
}
static int firstMissingPositive(int nums[],int length) {
int i=0;
while(i<length){
int correct=nums[i]-1;
if(nums[i] > 0 && nums[i]<length && nums[i]!=nums[correct])
swap(nums,i,correct);
else
i++;
}
for(int j=0;j<length;j++){
if(nums[j]!=j+1)
return j+1; // missing positive number
}
return length+1; // edge case: when last number is missing
}
int main() {
int nums[]={3,4,-1,1};
int len=sizeof(nums)/sizeof(nums[0]);
cout<<(firstMissingPositive(nums,len));
}
// Time Complexity: O(n)
// Space Complexity: O(1)