-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProblem_4.cpp
More file actions
44 lines (40 loc) · 1.18 KB
/
Problem_4.cpp
File metadata and controls
44 lines (40 loc) · 1.18 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
38
39
40
41
42
43
44
/*
* This problem was asked by Stripe.
* Given an array of integers, find the first missing positive integer in linear time and constant space.
* In other words, find the lowest positive integer that does not exist in the array.
* The array can contain duplicates and negative numbers as well.
*/
#include<bits/stdc++.h>
using namespace std;
long long separate(long long array[] , long long size){
long long j = 0;
for(long long i = 0;i<size;i++){
if(array[i] <= 0){
swap(array[i] , array[j]);
j++;
}
}
return j;
}
long long missingNumber(long long array[] , long long size){
for(long long i = 0;i<size;i++){
if((abs(array[i])-1 < size) and (array[abs(array[i])-1] > 0)){
array[abs(array[i])-1] = -array[abs(array[i])-1];
}
}
for(long long i = 0;i<size;i++){
if(array[i] > 0){
return i+1;
}
}
return size+1;
}
int main()
{
long long a[] = {1,2,0};
long long size = sizeof(a)/sizeof(a[0]);
long long start_positive = separate(a , size);
long long number = missingNumber(a+start_positive , size-start_positive);
cout<<endl;
cout<<number<<endl;
}