Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bit-masking problem #866

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions C++ Programs/two non-repeating element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// find two non-repeating elements in an array using bit-masking
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;

// } Driver Code Ends
class Solution
{
public:
vector<int> singleNumber(vector<int> nums)
{
// Code here.
int val =0;
for(int i=0;i<nums.size();i++)
{
val^=nums[i];
}
int rmsp = val;
rmsp = rmsp&~(rmsp-1);
int x=0,y=0;
for(int i=0;i<nums.size();i++)
{
if(rmsp &nums[i]==0)
{
x = x^nums[i];
}
else
{
y =y^nums[i];
}
}
vector <int>v;
v.push_back(x);
v.push_back(y);
return v;



}
};

// { Driver Code Starts.
int main(){
int T;
cin >> T;
while(T--)
{
int n;
cin >> n;
vector<int> v(2 * n + 2);
for(int i = 0; i < 2 * n + 2; i++)
cin >> v[i];
Solution ob;
vector<int > ans = ob.singleNumber(v);
for( int i=0;i<ans.size();i++)
cout << i << " ";
cout << "\n";
}
return 0;
} // } Driver Code Ends