-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.cpp
76 lines (66 loc) · 2.08 KB
/
13.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
// find unique no uisng bit manipulation
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int findUni(int a[],int n){
int xorsum=0;
for(int i=0;i<n;i++){
xorsum=xorsum^a[i];
}
return xorsum;
}
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<findUni(a,n)<<endl;
return 0;
}
// find two non repeating unique number using bit manipulation
// now we have find the right most bit now its time to seprate array into difft domains to find two unique no
// as they are unique so obviously as we get rightmost set bit as 1 so one will be 1 and other will be 0
// thats why we have decided to seprate it into two parts one with rightmost bit as 1 and other with 0
// and to differentaiate them in grp we will do & of these no with rightbit bcoz doing and will return that same no
// if done with 1 eg 1&1=1 0&1=0 so if we do & with 0 of our set bit that means it is not set and is off therefore
// it will be another grp and if not it is set and then will be sent to another grp
// after segregation of grp xor will be done of both grp now only one elemnt out of each grp will be there
// hence printing two unique element
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void findUni(int a[],int n){
int xorsum=0;
for(int i=0;i<n;i++){
xorsum=xorsum^a[i];
}
// now we want to get the rightmost set bit
// this can only be done in java so see about how we can implement to find rightmost bit in c++
// use brackets it is very necessary it may lead to error
int rightBit = xorsum & (-xorsum);
int x=0;
int y=0;
for(int i=0;i<n;i++){
if((a[i] & rightBit) == 0){
x=x^a[i];
}
else{
y=y^a[i];
}
}
cout<<x<<" "<<y;
}
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
findUni(a,n);
return 0;
}