Author: Anurag dubey
Tester: anurag dubey
Editorialist: anurag dubey
DIFFICULTY:
CAKEWALK, SIMPLE, EASY.
PREREQUISITES:
Math .greedy,bruteforce
PROBLEM:
In given problem array is given in that array we have just find out unique number that is the number that occurs once.
QUICK EXPLANATION:
Array is given and in that array except one number all the numbers occurs twice so , we will xor all the number in that array with each other then the unique number will remain in last.
EXPLANATION:
Since in the given array if we will xor all the element with each other then in last the number which will ocuurs once will remains in last.
for examaple in array 1 2 3 1 2
if we will xor 1 with 2 we will get 3 then we will xor it with next number 3 then 0 will remain and then we will xor 0 with 1 then 1 will remain and in last 1 will xor with 2 and final ans will be found that is 3.
SOLUTIONS:
Setter's Solution
-
#include<bits/stdc++.h>
-
using namespace std;
-
void testcase()
-
{
-
int n;
-
cin>>n;
-
int a[n];
-
for(int i=0;i<n;i++){
-
cin>>a[i];
-
}
-
int unique=0;
-
for(int i=0;i<n;i++){
-
unique=(unique^a[i]);
-
}
-
cout<<unique;
-
}
-
int main()
-
{
-
int t;
-
cin>>t;
-
while(t–)
-
{
-
testcase();
-
cout<<endl;
-
}
-
return 0;
-
}
Tester's Solution
-
#include<bits/stdc++.h>
-
using namespace std;
-
void testcase()
-
{
-
int n;
-
cin>>n;
-
int a[n];
-
for(int i=0;i<n;i++){
-
cin>>a[i];
-
}
-
int unique=0;
-
for(int i=0;i<n;i++){
-
unique=(unique^a[i]);
-
}
-
cout<<unique;
-
}
-
int main()
-
{
-
int t;
-
cin>>t;
-
while(t–)
-
{
-
testcase();
-
cout<<endl;
-
}
-
return 0;
-
}
Editorialist's Solution
-
#include<bits/stdc++.h>
-
using namespace std;
-
void testcase()
-
{
-
int n;
-
cin>>n;
-
int a[n];
-
for(int i=0;i<n;i++){
-
cin>>a[i];
-
}
-
int unique=0;
-
for(int i=0;i<n;i++){
-
unique=(unique^a[i]);
-
}
-
cout<<unique;
-
}
-
int main()
-
{
-
int t;
-
cin>>t;
-
while(t–)
-
{
-
testcase();
-
cout<<endl;
-
}
-
return 0;
-
}