forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind Missing And Repeating.cpp
82 lines (70 loc) · 1.67 KB
/
Find Missing And 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
71
72
73
74
75
76
77
78
79
80
81
82
/*
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.
Example 1:
Input:
N = 2
Arr[] = {2, 2}
Output: 2 1
Explanation: Repeating number is 2 and
smallest positive missing number is 1.
Example 2:
Input:
N = 3
Arr[] = {1, 3, 3}
Output: 3 2
Explanation: Repeating number is 3 and
smallest positive missing number is 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function findTwoElement() which takes the array of integers arr and n as parameters and returns an array of integers of size 2 denoting the answer ( The first index contains B and second index contains A.)
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 105
1 ≤ Arr[i] ≤ N
*/
class Solution
{
public:
int *findTwoElement(int *arr, int n)
{
int XOR = 0;
for (int i = 0; i < n; ++i)
XOR ^= ((i + 1) ^ arr[i]);
int setbit_no = XOR & ~(XOR - 1);
int bucket1 = 0, bucket2 = 0;
for (int i = 0; i < n; ++i)
{
if ((arr[i] & setbit_no))
bucket1 ^= arr[i];
else
bucket2 ^= arr[i];
if (((i + 1) & setbit_no))
bucket1 ^= (i + 1);
else
bucket2 ^= (i + 1);
}
int *ans = new int[2];
int flag = 0;
for (int i = 0; i < n; ++i)
{
if (arr[i] == bucket1)
{
flag = 1;
break;
}
}
if (flag)
{
ans[0] = bucket1;
ans[1] = bucket2;
}
else
{
ans[1] = bucket1;
ans[0] = bucket2;
}
return ans;
}
};