Skip to content

Commit fca93f0

Browse files
Create two odd occurrence.cpp
You are given an array of numbers and all numbers except two have even occurrences. Find the two numbers having odd occurrences
1 parent c483002 commit fca93f0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
/You are given an array of numbers and all numbers except two have even occurrences. Find the two numbers having odd occurrences
5+
//ex. a[8]={1,1,2,3,2,5,2,2} ans=3,5;
6+
void two_odd_occurrence(int a[],int t){
7+
int i,res=0,res1=0,res2=0;
8+
for(i=0;i<t;i++){
9+
res=res^a[i];
10+
}
11+
res=res&(~(res-1));
12+
for(i=0;i<t;i++){
13+
if((res&a[i])!=0){
14+
res1=res1^a[i];
15+
}
16+
else{
17+
res2=res2^a[i];
18+
}
19+
}
20+
cout<<res1<<" "<<res2<<endl;
21+
}
22+
int main(){
23+
int t,i;
24+
cin>>t;
25+
int a[t];
26+
for(i=0;i<t;i++){
27+
cin>>a[i];
28+
}
29+
two_odd_occurrence(a,t);
30+
}

0 commit comments

Comments
 (0)