Skip to content

Commit 49d70c3

Browse files
committed
Non repeating numbers using C++
1 parent 16522eb commit 49d70c3

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
/*
3+
Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers.
4+
5+
6+
Example 1:
7+
8+
Input:
9+
N = 2
10+
arr[] = {1, 2, 3, 2, 1, 4}
11+
Output:
12+
3 4
13+
Explanation:
14+
3 and 4 occur exactly once.*/
15+
#include<bits/stdc++.h>
16+
using namespace std;
17+
18+
19+
class Solution
20+
{
21+
public:
22+
vector<int> singleNumber(vector<int> nums)
23+
{
24+
// Code here.
25+
int res=0;
26+
vector <int> vec,v;
27+
for(int i=0;i<nums.size();i++)
28+
{
29+
res^=nums[i];
30+
31+
}
32+
int set_bit_no = res & ~(res-1);
33+
int temp=res;
34+
for(int i=0;i<nums.size();i++)
35+
{
36+
if((nums[i]& set_bit_no))
37+
{
38+
vec.push_back(nums[i]);
39+
}
40+
}
41+
for(int i=0;i<vec.size();i++)
42+
{
43+
temp^=vec[i];
44+
}
45+
v.push_back(temp);
46+
res^=temp;
47+
v.push_back(res);
48+
sort(v.begin(),v.end());
49+
return v;
50+
51+
}
52+
};
53+
54+
// { Driver Code Starts.
55+
int main(){
56+
int T;
57+
cin >> T;
58+
while(T--)
59+
{
60+
int n;
61+
cin >> n;
62+
vector<int> v(2 * n + 2);
63+
for(int i = 0; i < 2 * n + 2; i++)
64+
cin >> v[i];
65+
Solution ob;
66+
vector<int > ans = ob.singleNumber(v);
67+
for(auto i: ans)
68+
cout << i << " ";
69+
cout << "\n";
70+
}
71+
return 0;
72+
} // } Driver Code Ends

0 commit comments

Comments
 (0)