-
Notifications
You must be signed in to change notification settings - Fork 0
/
50.cpp
94 lines (68 loc) · 1.59 KB
/
50.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
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
using namespace std;
// Power of XOR Operator
// Swapping of two numbers using XOR
// int main() {
// int a = 4, b = 6;
// /*
// 1 0 --> 1
// 0 1 --> 1
// 0 0 --> 0
// 1 1 --> 0
// --> XOR of two numbers is 0 ( x ^ x == 0 )
// 101
// 101 ^
// ----
// 000
// --> XOR of a number with 0 is the number itself ( x^0 == x)
// 101
// 000 ^
// ------
// 101
// --> XOR is associative thus order does not matter
// x^y^z == y^z^x == z^y^x
// */
// a = a ^ b;
// b = b ^ a;
// a = a ^ b;
// /*
// b = b ^ ( a ^ b )
// b = b ^ b ^ a
// b = 0 ^ a
// b = a
// and
// a = a ^ b;
// a = ( a ^ b) ^ a; //b->a due to second step
// a = a ^ a ^ b;
// a = 0 ^ b;
// a = b;
// */
// cout<<a<<" "<<b<<endl;
// return 0;
// }
/*
Given array of n integers. All integers are present in event count except one.
Find that one integer which has odd count in O(N) time complexity and O(1) space;
N<10^5
a[i]<10^5
input
9
2 4 6 7 7 4 2 2 2
*/
int main(){
/*
c ^ a ^ b ^ c ^ b --> c ^ a ^ c ^ b ^ b --> c ^ a ^ c ^ 0 --> a ^ c ^ c --> a
thus no. which are present in even count, there xor would be cancelled
in last only that element will be left which is present in odd count
*/
int n;
cin>>n;
int x;
int ans = 0; // xor of any number with 0 is that number itself
for (int i = 0; i < n; i++)
{
cin>>x;
ans ^= x;
}
cout<<ans<<endl;
}