Unique_num , https://www.codechef.com/BEST2021/problems/UNIQUE01

Practice

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
  1. #include<bits/stdc++.h>

  2. using namespace std;

  3. void testcase()

  4. {

  5. int n;

  6. cin>>n;

  7. int a[n];

  8. for(int i=0;i<n;i++){

  9. cin>>a[i];

  10. }

  11. int unique=0;

  12. for(int i=0;i<n;i++){

  13. unique=(unique^a[i]);

  14. }

  15. cout<<unique;

  16. }

  17. int main()

  18. {

  19. int t;

  20. cin>>t;

  21. while(t–)

  22. {

  23. testcase();

  24. cout<<endl;

  25. }

  26. return 0;

  27. }

Tester's Solution
  1. #include<bits/stdc++.h>

  2. using namespace std;

  3. void testcase()

  4. {

  5. int n;

  6. cin>>n;

  7. int a[n];

  8. for(int i=0;i<n;i++){

  9. cin>>a[i];

  10. }

  11. int unique=0;

  12. for(int i=0;i<n;i++){

  13. unique=(unique^a[i]);

  14. }

  15. cout<<unique;

  16. }

  17. int main()

  18. {

  19. int t;

  20. cin>>t;

  21. while(t–)

  22. {

  23. testcase();

  24. cout<<endl;

  25. }

  26. return 0;

  27. }

Editorialist's Solution
  1. #include<bits/stdc++.h>

  2. using namespace std;

  3. void testcase()

  4. {

  5. int n;

  6. cin>>n;

  7. int a[n];

  8. for(int i=0;i<n;i++){

  9. cin>>a[i];

  10. }

  11. int unique=0;

  12. for(int i=0;i<n;i++){

  13. unique=(unique^a[i]);

  14. }

  15. cout<<unique;

  16. }

  17. int main()

  18. {

  19. int t;

  20. cin>>t;

  21. while(t–)

  22. {

  23. testcase();

  24. cout<<endl;

  25. }

  26. return 0;

  27. }

Atleast editorials must be properly formatted :slight_smile: Also, please change the topic name.

1 Like

its my first tutorial manvi_03 if you does’nt understood then tell me i will explain in another way thanks for your suggestion bt next time i will be try to do better

1 Like

If you have any idea about it please tell me about it

tell me your problem in detail i will try to solve it

1 Like

@anurag_ad_01 the only problem I faced in your editorial was that the solution that you had given was something like this:

Your code:

And this is what I meant by formatting the text.

Formatted Code
 #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;
}

All that it shall do is make your code look good and understandable :slightly_smiling_face:

2 Likes

ohh got it ma’am sorry !
you just talking about only formatting :sweat_smile:
and i am thinking about tutorial .
ok manvi i will format next time properly .
and one more thing come in my next contest JARVIS_2.O which is on june 1 ok please come .

1 Like

It’s totally alright! Thanks for invitation. I’ll turn up for sure! :grinning_face_with_smiling_eyes:

CodeChef: Practical coding for everyone please tell me why does this code give me a wrong answer??

1 Like

You have to initialise ans to 0 for every test case.
Your code, ACfied.

Bro you have to intiallise ans=0 in the while loop that is for every testcase your answer should be started from 0 ok

i 'll wait :innocent:

I don’t see JARVIS_2.O in the list of present or upcoming contests.