-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path260 Single Number III.cpp
More file actions
32 lines (31 loc) · 1.01 KB
/
260 Single Number III.cpp
File metadata and controls
32 lines (31 loc) · 1.01 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
static int fastio=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
// Pass 1 :
// Get the XOR of the two numbers we need to find get xor of all elements of vector
long long int diff = accumulate(nums.begin(), nums.end(), 0, bit_xor<int>());
// Get its last set bit
diff &= -diff; // ~(diff - 1) = - (diff - 1) - 1 = -diff
// Pass 2 :
vector<int> rets = {0, 0}; // this vector stores the two numbers we will return
for (int num : nums)
{
//Since, only one pair has this particular bit set or not set others willcancel out
if ((num & diff) == 0) // the bit is not set
{
rets[0] ^= num;// other will cancel out
}
else // the bit is set
{
rets[1] ^= num;// other will cancel out
}
}
return rets;
}
};