forked from ezaz039/Hactoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmallestPositiveMissingNumber.cpp
More file actions
55 lines (46 loc) · 1.02 KB
/
Copy pathsmallestPositiveMissingNumber.cpp
File metadata and controls
55 lines (46 loc) · 1.02 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
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.
class Solution
{
public:
//Function to find the smallest positive number missing from the array.
int missingNumber(int arr[], int n)
{
unordered_map<int,int> mpp;
for(int i=0;i<n;i++)
{
if(arr[i] > 0)
{
mpp[arr[i]]++;
}
}
for(int i=1;i<n;i++)
{
if(mpp.find(i) == mpp.end())
{
return i;
}
}
if(arr[0] == 0)
{
return n;
}
else if(n==1 && arr[0] > 1){
return 1;
}
return n+1;
}
};