XOREngine problem in March long challenge (2020)

hi , i am new to codechef . I have participated in march long challenge .
I want to ask about a problem that was asked in the challenge -> XORengine .
See my code once that i have tried , please tell me why its not working .

using namespace std;
#define loop(i,n) for(int i=0;i<n;i++)
int main(void){
   int t;
   cin>>t;
   while(t- -){
       int n,q;
       cin>>n>>q;
       int* ar = new int[n];
       loop(i,n){
           cin>>ar[i];
       }
       while(q- -){
           int p;
           cin>>p;
           loop(i,n){
               ar[i] = (p ^ ar[i]);
           }
           int counteve=0;
           int countodd=0;
          loop(i,n){
              int val = ar[i];
              int tempcount=0;
              for(int j=0;j<(32);j++){
                  if(val&(1<<j)){
                      tempcount++;
                  }
              }
              if(tempcount%2==0){
                  counteve++;
              }else{
                  countodd++;
              }
          }
          cout<<counteve<< " "<<countodd<<endl;
       }
   }

hey @hackinet i used t - - only but here due to editor of codechef it seems to be hyphen .
So try getting me whats wrong in the code instead of arguing with me . now i have given more space between them , try to see it carefully .

1 Like

@anon86902332 My apologies. Do you get TLE or WA?

1 Like

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

2 Likes

@hackinet i was getting WA .

Testcase:

1
1 2
9
7
6

Your Output:

0 1
0 1

The correct output(what the output should be):

0 1
1 0
1 Like

hey i can get that there exist some output mismatch with my code thats why i got WA . but can you get me that what is the logical error in my code . As in my code i am check each bit of the each element and then i am keeping count of 1’s and thus at last i am determining
the even / odd . Can you tell where it is going wrong ??

Yes.

 while(q- -){
           int p;
           cin>>p;
           loop(i,n){
               ar[i] = (p ^ ar[i]); //<- Your Error.
           }
           int counteve=0;
  1. You take a input p and XOR it with all the elements of A[i].
  2. A[i] now contains different elements. NOT the original elements.
  3. You take input p again but this time, you won’t have the original A[i], because during previous p input you updated all the A[i].
1 Like

okay :slight_smile: thanks @hackinet . I got my mistake .

1 Like