-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatandMissingNumberArray.cpp
More file actions
60 lines (41 loc) · 1.23 KB
/
RepeatandMissingNumberArray.cpp
File metadata and controls
60 lines (41 loc) · 1.23 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
56
57
58
59
60
/*
You are given a read only array of n integers from 1 to n.
Each integer appears exactly once except A which appears twice and B which is missing.
Return A and B.
Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Note that in your output A should precede B.
Example:
Input:[3 1 2 5 3]
Output:[3, 4]
A = 3, B = 4
LINK: https://www.interviewbit.com/problems/repeat-and-missing-number-array/
*/
vector<int> Solution::repeatedNumber(const vector<int> &A) {
int n = A.size();
long long int s1=0, s2=0;
for(long long int i = 0; i<n;i++){
long long int x = A[i];
s1 += (i+1) - x;
s2 += (i+1)*(i+1) - x*x;
}
int M,R;
M = (s1 + (s2/s1))/2;
R = M - s1;
return {R, M};
}
/*
Method 7 (Make two equations using sum and sum of squares)
Approach:
Let x be the missing and y be the repeating element.
Let N is the size of array.
Get the sum of all numbers using formula S = N(N+1)/2
Get product of all numbers using formula Sum_Sq = N(N+1)(2N+1)/6
Iterate through a loop from i=1….N
S -= A[i]
Sum_Sq -= (A[i]*A[i])
It will give two equations
x-y = S – (1)
x^2 – y^2 = Sum_sq
x+ y = (Sum_sq/S) – (2)
Time Complexity: O(n)
*/