File tree Expand file tree Collapse file tree 1 file changed +79
-0
lines changed
Expand file tree Collapse file tree 1 file changed +79
-0
lines changed Original file line number Diff line number Diff line change 1+ /* Title : User will enter the unsorted array .
2+ You will need to sort it and has to
3+ find out the first missing positive integer from the sorted array .
4+ */
5+ #include < iostream>
6+ #include < math.h>
7+ #include < stdlib.h>
8+ using namespace std ;
9+ int main ()
10+ {
11+ int n ,j=0 , i,k=1 , val;
12+ cout<<" Enter the total number of elements " <<endl;
13+ cin>>n;
14+ int a[n];
15+ cout<<" Enter the elements : \n " <<endl;
16+ for (i=0 ;i<n;i++)
17+ { cin>>a[i]; }
18+
19+ for (i=0 ;i<n;i++)
20+ { for (j=0 ;j<n-1 ;j++)
21+ if (a[j]>a[j+1 ])
22+ { val=a[j];
23+ a[j]=a[j+1 ];
24+ a[j+1 ]=val; }
25+ }
26+ cout<<" Sorted array : \n " <<endl;
27+ for (i=0 ;i<n;i++)
28+ {
29+ cout<<a[i]<<" " ;
30+ }
31+ cout<<" \n " <<endl;
32+ cout<<" Missing element : " ;
33+ if (a[n-1 ]<=0 )
34+ {
35+ cout<<" 1" ;
36+
37+ }
38+ else {
39+
40+ for (i=0 ;i<n;i++)
41+ {
42+ if (a[i]>0 )
43+ {
44+ for (j=0 +i, k=1 ;j<=n && k<=n+1 ;k++,j++)
45+ {
46+ if (a[j]==k)
47+ {
48+ continue ;
49+ }
50+ else
51+ {
52+
53+ cout<<k;
54+ exit (0 );
55+ } }
56+ } }
57+ }}
58+
59+
60+
61+
62+
63+
64+ /*
65+ Enter the total number of elements
66+ 5
67+ Enter the elements :
68+
69+ 12
70+ -43
71+ -5
72+ 0
73+ 2
74+ Sorted array :
75+
76+ -43 -5 0 2 12
77+
78+ Missing element : 1
79+ */
You can’t perform that action at this time.
0 commit comments