-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#4.cpp
119 lines (102 loc) · 2.53 KB
/
#4.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Problem Description:
Good morning! Here's your coding interview problem for today.
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.
For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.
You can modify the input array in-place.
*/
#include <bits/stdc++.h>
using namespace std;
// Brute force approach
int firstMissingPosI(int arr[], int n)
{
int ref = 1;
// Maximizing Searches for the ref value
for (int i = 0; i < n; i++)
{
// Search for the ref value if found increment it
for (int j = 0; j < n; j++)
{
if (arr[j] == ref)
ref += 1;
}
}
return ref;
}
// Sorting arrpoach
int firstMissingPos2(int arr[], int n)
{
sort(arr, arr + n);
int ref = 1;
for (int i = 0; i < n; i++)
{
if (arr[i] > 0)
{
if (arr[i] == ref)
ref += 1;
// Handling Duplicates
else if (arr[i] == ref - 1)
continue;
else
return ref;
}
}
return ref;
}
// Hashing - Sorting done using extra space in O(n)
int firstMissingPos3(int arr[], int n)
{
unordered_set<int> s;
for (int i = 0; i < n; i++)
{
if (arr[i] > 0)
s.insert(arr[i]);
}
int ref = 1;
for (auto i = s.begin(); i != s.end(); i++)
{
if ((*i) == 1)
ref += 1;
else if ((*i) == ref - 1)
continue;
else
return ref;
}
return ref;
}
// Solution we're looking for: O(n) linear time and O(1) extra space
// int segregate(int arr[], int n)
// {
// int j = 0;
// for (int i = 0; i < n; i++)
// {
// if (arr[i] <= 0)
// {
// swap(arr[i], arr[j]);
// j++;
// }
// }
// return j;
// }
// int firstMissingPos4(int arr[], int n)
// {
// int i = segregate(arr, n);
// for(; i < n; i++){
// int abs_val = abs(arr[i]);
// int idx = abs_val - 1;
// if(idx < n && idx > 0) arr[idx] = -arr[idx];
// }
// for(int i = 0; i < n; i++){
// if(arr[i] > 0) return i + 1;
// }
// return n + 1;
// }
int main()
{
int arr[] = {1, 1, 0, -1, -2};
int n = sizeof(arr) / sizeof(arr[0]);
cout << firstMissingPos3(arr, n) << endl;
}