-
Notifications
You must be signed in to change notification settings - Fork 170
/
missing_repeating.cpp
70 lines (60 loc) · 1.85 KB
/
missing_repeating.cpp
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
63
64
65
66
67
68
69
70
/*
problem link: https://www.youtube.com/redirect?event=video_description&redir_token=QUFFLUhqbTBURkpGb1l5X3lzMC1oS1dHUVBuRVlZRlp4d3xBQ3Jtc0tuYXV3YmQybmY0QWc2NkUyeHZNUUVNcUFVSEpHdVQtcVR4RDFtYUQ3eWJSendVMWhRUG8yamVJMWFMTWhZZ1hHSDNrUnFpZHhxa3dRcFB0VzNHaWJNeWNSVllQa0x5cDZtTjRSTGpXVk8ya1VuZy0zYw&q=https%3A%2F%2Fwww.geeksforgeeks.org%2Ffind-a-repeating-and-a-missing-number%2F
*/
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
int *findTwoElement(int *arr, int n) {
// code here
int result = 0;
for(int i = 0; i < n; i++)
result ^= arr[i];
// take xor with 1,2,3...n
for(int i = 1; i <= n; i++)
result ^= i;
// result = missing ^ repeating
int rsb_mask = result & -result;
int missing = 0, repeating = 0;
// partition elements into 2 sets
for(int i = 0; i < n; i++) {
if(rsb_mask & arr[i])
repeating ^= arr[i];
else
missing ^= arr[i];
}
for(int i = 1; i <= n; i++) {
if(rsb_mask & i)
repeating ^= i;
else
missing ^= i;
}
// find the exact values of missing and repeating
for(int i = 0; i < n; i++) {
if(arr[i] == missing) {
swap(missing, repeating);
break;
}
}
int *ans = new int[2];
ans[0] = repeating;
ans[1] = missing;
return ans;
}
};
int main() {
int t;
cin >> t;
while (t--) {
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;
}