forked from Amisha-here/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find_Unique_Number
64 lines (60 loc) · 933 Bytes
/
Find_Unique_Number
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
//We are given an array containg n numbers. All the numbers are present thrice except for one number which is only present once. Find the unique number. Only use - bitwise operators, and no extra space.
//
//Input Format
//First line contains the number n. Second line contains n space separated number.
//
//Constraints
//N < 10^5
//
//Output Format
//Output a single line containing the unique number
//
//Sample Input
//7
//1 1 1 2 2 2 3
//Sample Output
//3
//Explanation
//3 is present only once
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int Count_Bits(int N)
{
int C;
C=0;
while(N>0)
{
++C;
N=N>>1;
}
return C;
}
int main() {
int T,M,X,Y,Z,i;
cin>>T;
int A[T];
M=0;
for(i=0;i<T;i++)
{
cin>>A[i];
M=max(M,A[i]);
}
M=Count_Bits(M);
X=1;
Y=0;
while(M--)
{
Z=0;
for(i=0;i<T;i++)
{
if(X&A[i])
++Z;
}
Y=Y+(Z%3)*X;
X=X<<1;
}
cout<<Y<<endl;
return 0;
}