-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindMissingAndRepeatingElement.cpp
More file actions
53 lines (46 loc) · 1.21 KB
/
findMissingAndRepeatingElement.cpp
File metadata and controls
53 lines (46 loc) · 1.21 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
/* 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. */
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
int *findTwoElement(int *arr, int n)
{
int c,i=1;
int *a=new int[2];
//iterating form i=0 to N
while(i<=n)
{
//c-count occurance of i in array
c=0;
for(int j=0;j<n;j++)
{
//when i element is found in array then increase its occurance
if(arr[j]==i)
c++;
}
//if i occur 2 times then its duplicat elements
if(c==2)
a[0]=i;
//if i isnt found the its missing element
else if(c==0)
a[1]=i;
i++;
}
return a;
}
};
int main() {
//n-array size
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
Solution ob;
auto ans = ob.findTwoElement(a, n);
cout << ans[0] << " " << ans[1] << "\n";
return 0;
}