304 North Cardinal St.
Dorchester Center, MA 02124

# XOR Engine CodeChef Solution

## XOR Engine CodeChef Solution in C++17

``````#include <bits/stdc++.h>

using namespace std;
#define Loma ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ln "\n"
#define lli long long int
const int N=1e5;

int a[N],even,odd,p,c,x;

void solve()
{
even=0,odd=0,c=0,x;
int n,q;cin>>n>>q;
for(int i=0;i<n;i++)
{
c=0;
cin>>a[i];
x=a[i];
while(x)
{
if(x&1)
c++;
x>>=1;
}
even+=(c%2==0);
odd+=(c%2);
}
while(q--)
{
c=0;
cin>>p;
x=p;
while(x)
{
if(x&1)
c++;
x>>=1;
}
if(c%2)
{
cout<<odd<<" "<<even<<ln;
}
else
{
cout<<even<<" "<<odd<<ln;
}
}

}

int main()
{
Loma;
int t;
cin>>t;
while(t--)
solve();
return 0;
}``````

## XOR Engine CodeChef Solution in C++14

``````#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
int t;cin>>t;
int even=0,odd=0;
while(t--){
int n,q;

scanf("%d", &n);
scanf("%d", &q);
int odd= 0 ,even = 0 ;
for (int i = 0 ;i<n; i++){
int temp;
scanf("%d", &temp);
int res = __builtin_popcount(temp);
if (res&1) odd++;
else even++;
}
while(q--){
int temp;
scanf("%d", &temp);
int res= __builtin_popcount(temp);
if(res&1){
printf("%d ",odd);
printf("%d\n",even);

}
else
{
printf("%d ",even);
printf("%d\n",odd);

}
}

}
return 0;
}``````

## XOR Engine CodeChef Solution in PYTH 3

``````import sys

def xor_engine():
for _ in range(t):
numbers_even_ones_count, numbers_odd_ones_count = 0, 0

for number in numbers:
if bin(number).count('1') % 2 == 0:
numbers_even_ones_count += 1

numbers_odd_ones_count = n - numbers_even_ones_count

for ___ in range(q):
if numbers_odd_ones_count == numbers_even_ones_count:
sys.stdout.write(f'{numbers_even_ones_count} {numbers_odd_ones_count}\n')
continue

are_p_ones_even = bin(p).count('1') % 2 == 0
if are_p_ones_even:
sys.stdout.write(f'{numbers_even_ones_count} {numbers_odd_ones_count}\n')
else:
sys.stdout.write(f'{numbers_odd_ones_count} {numbers_even_ones_count}\n')

xor_engine()``````

## XOR Engine CodeChef Solution in C

``````#include<stdio.h>
int main()
{
long int t,i;
scanf("%ld",&t);
for(i=0;i<t;i++)
{
long int n,q,j,k;
scanf("%ld %ld",&n,&q);
long int a[n],b[n],fc1=0,fc2=0;
for(j=0;j<n;j++){
scanf("%ld",&a[j]);
}
for(j=0;j<q;j++)
{
long int p,r,c;
scanf("%ld",&p);
if(j==0){
for(k=0;k<n;k++)
b[k]=p^a[k];
}
if(j==0)
{
for(k=0;k<n;k++)
{
c=0;
while(b[k]!=0)
{
r=b[k]%2;
if(r==1)
c++;
b[k]=b[k]/2;
}
if(c%2==0||c==0)
fc1++;
else
fc2++;
}
int temp=p;
c=0;
while(temp!=0)
{
r=temp%2;
if(r==1)
c++;
temp=temp/2;
}
if(c%2!=0)
{
int swap=fc1;
fc1=fc2;
fc2=swap;
printf("%ld %ld\n",fc2,fc1);
}
else
printf("%ld %ld\n",fc1,fc2);
}
int temp=p;
c=0;
if(j>0)
{
while(temp!=0)
{
r=temp%2;
if(r==1)
c++;
temp=temp/2;
}
//printf("%ld",c);
if(c%2==0)
printf("%ld %ld\n",fc1,fc2);
else
printf("%ld %ld\n",fc2,fc1);
}
}
}
return 0;
}``````

## XOR Engine CodeChef Solution in JAVA

``````/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
StringBuilder sb = new StringBuilder();

for(int tc1=0;tc1<tc;tc1++)
{

int n=Integer.parseInt(s1[0]);
int q=Integer.parseInt(s1[1]);

int a[]=new int[n];
int even=0,odd=0;

for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(s2[i]);

if(isEven(a[i]))
even++;
else
odd++;
}

for(int i=0;i<q;i++)
{
if(isEven(p))
sb.append(even+" "+odd+"\n");
else
sb.append(odd+" "+even+"\n");
}
}
System.out.println(sb);
}
public static boolean isEven(int n)
{
int setBits=0;
while(n!=0)
{
if((n&1)==1)
setBits++;
n>>=1;
}
if((setBits&1)==0)
return true;
else return false;
}
}``````

## XOR Engine CodeChef Solution in PYPY 3

``````import io, os
import sys

t = int(input().decode())

for _ in range(t):
mod = 10**9 + 7
n, q = list(map(int, input().decode().split()))
A = list(map(int, input().decode().split()))
odd = 0
even = 0
for ele in A:

count = bin(ele).count("1")
if count%2  == 0:
even += 1
else:
odd += 1

for i in range(q):
p = int(input().decode())
# 		x = bin(p)[2:]
count = bin(p).count("1")
if count%2 == 0:
sys.stdout.write(str(even)+ " " + str(odd)  +"\n")
else:
sys.stdout.write(str(odd)+ " " + str(even)  +"\n")``````

## XOR Engine CodeChef Solution in PYTH

``````# cook your dish here
from sys import stdin, stdout

def countSetBits(n):

count = 0
while (n):
n &= (n-1)
count+= 1
return count

T = int(input())
for _ in range(T):
A = (countSetBits(int(x)) for x in stdin.readline().split())
evenLen = len(list(i for i in A if (int(i) % 2 == 0)))
oddLen = N - evenLen
for _ in range(Q):
if(P % 2 == 0):
stdout.write(str(evenLen) + " " + str(oddLen) + "\n")
else:
stdout.write(str(oddLen) + " " + str(evenLen)+ "\n")``````

## XOR Engine CodeChef Solution in C#

``````using System;
using System.Collections.Generic;
using System.IO;

namespace XorEngine
{
public class Program
{

public static void Main(string[] args)
{

var output = new List<string>();
for (var testId = 0; testId < testCount; testId++)
{
var n = int.Parse(data[0]);
var q = int.Parse(data[1]);
var numberOfAWithAnEvenNumberOfBits = CountNumberWithAnEvenNumberOfBits(a);
var numberOfAWithAnOddNumberOfBits = n - numberOfAWithAnEvenNumberOfBits;
for (var queryId = 0; queryId < q; queryId++)
{
var pBitCount = CountOneBits(p);
}
}

Console.WriteLine(string.Join("\n", output));
}

private static int CountNumberWithAnEvenNumberOfBits(int[] numbers)
{
var result = 0;
for (var i = 0; i < numbers.Length; i++)
{
if (CountOneBits(numbers[i]) % 2 == 0)
result++;
}
return result;
}

{
var pBitCount = CountOneBits(p);
}

public static int CountOneBits(int n)
{
var count = 0;
while (n != 0)
{
count++;
n &= (n - 1);
}
return count;
}

{
}

{
}

static int[] FastReadLineAsIntArray(int n, string data)
{
var arr = new int[n];
var index = 0;
var current = 0;
var sign = 1;
foreach (var c in data)
{
if ('0' <= c && c <= '9')
{
current = current * 10 + (c - '0');
}
else if (c == '-')
{
sign = -1;
}
else
{
{
arr[index++] = sign * current;
current = 0;
sign = 1;
}
}
}
if (hasReadSomething) arr[index] = sign * current;
return arr;
}
}
}``````

## XOR Engine CodeChef Solution in GO

``````package main

import (
"bufio"
"bytes"
"fmt"
"os"
)

func readUint64(bytes []byte, from int, val *uint64) int {
i := from

var tmp uint64
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + uint64(bytes[i]-'0')
i++
}
*val = tmp

return i
}

func readInt(bytes []byte, from int, val *int) int {
i := from
sign := 1
if bytes[i] == '-' {
sign = -1
i++
}
tmp := 0
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + int(bytes[i]-'0')
i++
}
*val = tmp * sign
return i
}

for i := 0; i < len(s); i++ {
if s[i] == '\n' {
return s[:i]
}
}
return s
}

return
}

a, b = res[0], res[1]
return
}

a, b, c = res[0], res[1], res[2]
return
}

res := make([]int, n)
x := 0
for i := 0; i < n; i++ {
for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
x++
}
}
return res
}

func main() {

var buf bytes.Buffer

for tc > 0 {
tc--

solver := NewSolver(n, A)

for q > 0 {
q--
buf.WriteString(fmt.Sprintf("%d %d\n", a, b))
}
}
fmt.Print(buf.String())
}

const MAX_N = 100001

type Solver struct {
even, odd int
}

func NewSolver(n int, A []int) Solver {
var odd, even int

for i := 0; i < n; i++ {
var cnt int
for A[i] > 0 {
cnt += A[i] & 1
A[i] >>= 1
}
if cnt&1 == 1 {
odd++
} else {
even++
}
}

return Solver{odd, even}
}

func (solver Solver) Ask(p int) (x int, y int) {
var cnt int
for p > 0 {
cnt += p & 1
p >>= 1
}
// o + o => e
// o + e => o
// e + e => e
// e + o => o
if cnt&1 == 1 {
// odd
x = solver.even
y = solver.odd
} else {
x = solver.odd
y = solver.even
}
return
}``````
##### XOR Engine CodeChef Solution Review:

In our experience, we suggest you solve this XOR Engine CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the XOR Engine CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this XOR Engine CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!