-
Notifications
You must be signed in to change notification settings - Fork 0
/
MInimum positive element
55 lines (50 loc) · 1002 Bytes
/
MInimum positive element
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
#include <bits/stdc++.h>
int firstMissing(int a[], int n)
{
// Write your code here.
vector <int> arr ;
for(int i = 0 ; i < n ; i++)
{
arr.push_back(a[i]) ;
}
int min = INT_MAX ;
int max = INT_MIN ;
for(int i = 0 ; i < n ; i++)
{
if(arr[i] <= 0)
{
continue ;
}
else
{
if(arr[i] < min)
{
min = arr[i] ;
}
if(arr[i] > max)
{
max = arr[i] ;
}
}
}
// cout << max << min << endl ;
if(min > 1)
{
// cout << " hey" << endl ;
return 1 ;
}
sort(arr.begin(),arr.end()) ;
for(int i = 1; i < max ; i++)
{
int x = 1 ;
if(binary_search(arr.begin() , arr.end() , i ) )
{
continue ;
}
else
{
return i ;
}
}
return max + 1 ;
}