-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathMissing_integer.cpp
79 lines (66 loc) · 1.26 KB
/
Missing_integer.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
/* Title : User will enter the unsorted array .
You will need to sort it and has to
find out the first missing positive integer from the sorted array .
*/
#include<iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;
int main()
{
int n ,j=0, i,k=1 , val;
cout<<" Enter the total number of elements "<<endl;
cin>>n;
int a[n];
cout<<" Enter the elements : \n"<<endl;
for(i=0;i<n;i++)
{ cin>>a[i]; }
for(i=0;i<n;i++)
{ for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
{ val=a[j];
a[j]=a[j+1];
a[j+1]=val; }
}
cout<<" Sorted array : \n"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<" \n"<<endl;
cout<<" Missing element : ";
if(a[n-1]<=0)
{
cout<<"1";
}
else {
for(i=0;i<n;i++)
{
if(a[i]>0)
{
for(j=0+i, k=1;j<=n && k<=n+1;k++,j++)
{
if(a[j]==k)
{
continue ;
}
else
{
cout<<k;
exit(0);
} }
} }
}}
/*
Enter the total number of elements
5
Enter the elements :
12
-43
-5
0
2
Sorted array :
-43 -5 0 2 12
Missing element : 1
*/