-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathAnswer2.cpp
48 lines (47 loc) · 821 Bytes
/
Answer2.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
vector<int> Solution::repeatedNumber(const vector<int> &A) {
int n=A.size();
int x=0;
for(int i=0;i<n;i++){
x^=A[i];
x^=(i+1);
}
int i;
for(i=31;i>=0;i--){
if(x&(1<<i)){
break;
}
}
int p=0;
int q=0;
for(int j=0;j<n;j++){
if(A[j]&(1<<i)){
p^=A[j];
}
else{
q^=A[j];
}
if((j+1)&(1<<i)){
p^=(j+1);
}
else{
q^=(j+1);
}
}
vector<int> vec;
int flag=0;
for(int i=0;i<n;i++){
if(A[i]==p){
vec.push_back(p);
flag=1;
break;
}
}
if(flag==1){
vec.push_back(q);
}
else{
vec.push_back(q);
vec.push_back(p);
}
return vec;
}