-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSmallestPositiveMissingNumber.cpp
More file actions
62 lines (56 loc) · 1.32 KB
/
Copy pathSmallestPositiveMissingNumber.cpp
File metadata and controls
62 lines (56 loc) · 1.32 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* GFG
You are given an array arr[] of N integers including 0. The task is to find the smallest positive number missing from the array.
Example 1:
Input:
N = 5
arr[] = {1,2,3,4,5}
Output: 6
Explanation: Smallest positive missing
number is 6.
Example 2:
Input:
N = 5
arr[] = {0,-10,1,3,-20}
Output: 2
Explanation: Smallest positive missing
number is 2.
Your Task:
The task is to complete the function missingNumber() which returns the smallest positive missing number in the array.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 10^6
-10^6 <= arr[i] <= 10^6
*/
class Solution
{
public:
//Function to find the smallest positive number missing from the array.
int missingNumber(int arr[], int n)
{
// Your code here
for(int i=0;i<n;i++)
{
if(arr[i]<=0||arr[i]>n)
continue;
else
{
int t=arr[i]-1;
while(t>=0)
{
int temp=arr[t];
arr[t]=INT_MIN;
if(temp<=0||temp>n)
break;
t=temp-1;
}
}
}
for(int i=0;i<n;i++)
{
if(arr[i]!=INT_MIN)
return i+1;
}
return n+1;
}
};