forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Two_odd_occuring.cpp
56 lines (45 loc) · 889 Bytes
/
Two_odd_occuring.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
// Two ocurring number
//Time Complexity: O(n)
//Space Complexity: O(1)
/*Sample Input:
Example 1:-
Enter number of elements
8
2 2 3 8 4 4 1 1
Example 2:-
Enter number of elements
7
1 1 2 2 5 3 3
Sample Output:
Example 1:-
3 8
Example 2:-
5 0
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter number of elements \n";
cin>>n;
vector<int>arr;
int a;
for(int i=0;i<n;i++)
{
cin>>a;
arr.push_back(a);
}
int xors = 0, res1 = 0, res2 = 0;
for (int i = 0; i < n; i++)
xors = xors ^ arr[i];
int sn = xors & (~(xors - 1));
for (int i = 0; i < n; i++)
{
if ((arr[i] & sn) != 0)
res1 = res1 ^ arr[i];
else
res2 = res2 ^ arr[i];
}
cout << res1 << " " << res2;
}