-
Notifications
You must be signed in to change notification settings - Fork 0
/
findMissing.txt
47 lines (42 loc) · 1.13 KB
/
findMissing.txt
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
Find Missing And Repeating
Given an unsorted array Arr of size N of positive integers. One number 'A' from set {1, 2,....,N}
is missing and one number 'B' occurs twice in array.
Find these two numbers.
class Solution{
public:
int returnMissing(vector<int>arr,int n){
unordered_map<int,bool>mp;
for(int i=1; i<=n; i++){
mp[i] = false;
}
for(int i=0; i<arr.size(); i++){
mp[arr[i]] = true;
}
for(auto i:mp){
if(i.second ==false){
return i.first;
}
}
return 0;
}
int returnDuplicate(vector<int>arr,int n){
unordered_map<int,int>mp;
for(int i=0; i<n; i++){
mp[arr[i]]++;
}
for(auto i:mp){
if(i.second > 1){
return i.first;
}
}
return 0;
}
vector<int> findTwoElement(vector<int> arr, int n) {
vector<int>ans;
int repeat = returnDuplicate(arr,n);
int missing = returnMissing(arr,n);
ans.push_back(repeat);
ans.push_back(missing);
return ans;
}
};