XOR Engine CodeChef Solution

Problem -XOR Engine CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

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() {
	// your code goes here
	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():
    t = int(sys.stdin.readline().strip())
    for _ in range(t):
        n, q = map(int, sys.stdin.readline().strip().split())
        numbers = list(map(int, sys.stdin.readline().strip().split()))
        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):
            p = int(sys.stdin.readline().strip())
            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
	{
		// your code goes here
		InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();

        int tc=Integer.parseInt(br.readLine());

        for(int tc1=0;tc1<tc;tc1++)
        {
            String s1[]=br.readLine().split(" ");
            
            int n=Integer.parseInt(s1[0]);
            int q=Integer.parseInt(s1[1]);
            
            String s2[]=br.readLine().split(" ");
            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++)
            {
                int p=Integer.parseInt(br.readLine());
                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


input = io.BytesIO(os.read(0, \
         os.fstat(0).st_size)).readline
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):
    N, Q = map(int, stdin.readline().split())
    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):
        P = countSetBits(int(stdin.readline()))
        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
    {
        private static TextReader Reader;

        public static void Main(string[] args)
        {
            Reader = Console.In;

            var output = new List<string>();
            var testCount = ReadLineAsInt();
            for (var testId = 0; testId < testCount; testId++)
            {
                var data = ReadLineAsString().Split(' ');
                var n = int.Parse(data[0]);
                var q = int.Parse(data[1]);
                var a = FastReadLineAsIntArray(n, ReadLineAsString());
                var numberOfAWithAnEvenNumberOfBits = CountNumberWithAnEvenNumberOfBits(a);
                var numberOfAWithAnOddNumberOfBits = n - numberOfAWithAnEvenNumberOfBits;
                var answerIfPIsEven = $"{numberOfAWithAnEvenNumberOfBits} {numberOfAWithAnOddNumberOfBits}";
                var answerIfPIsOdd = $"{numberOfAWithAnOddNumberOfBits} {numberOfAWithAnEvenNumberOfBits}";
                for (var queryId = 0; queryId < q; queryId++)
                {
                    var p = ReadLineAsInt();
                    var pBitCount = CountOneBits(p);
                    output.Add((pBitCount % 2 == 0) ? answerIfPIsEven : answerIfPIsOdd);
                }
            }

            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;
        }

        public static string SolveFast(int p, string answerIfPIsEven, string answerIfPIsOdd)
        {
            var pBitCount = CountOneBits(p);
            return (pBitCount % 2 == 0) ? answerIfPIsEven : answerIfPIsOdd;
        }

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

        static int ReadLineAsInt()
        {
            return int.Parse(Reader.ReadLine());
        }

        static string ReadLineAsString()
        {
            return Reader.ReadLine();
        }

        static int[] FastReadLineAsIntArray(int n, string data)
        {
            var arr = new int[n];
            var index = 0;
            var current = 0;
            var hasReadSomething = false;
            var sign = 1;
            foreach (var c in data)
            {
                if ('0' <= c && c <= '9')
                {
                    hasReadSomething = true;
                    current = current * 10 + (c - '0');
                }
                else if (c == '-')
                {
                    sign = -1;
                }
                else
                {
                    if (hasReadSomething)
                    {
                        arr[index++] = sign * current;
                        current = 0;
                        sign = 1;
                        hasReadSomething = false;
                    }
                }
            }
            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
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' {
			return s[:i]
		}
	}
	return s
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func main() {
	scanner := bufio.NewReader(os.Stdin)

	tc := readNum(scanner)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n, q := readTwoNums(scanner)

		A := readNNums(scanner, n)

		solver := NewSolver(n, A)

		for q > 0 {
			q--
			p := readNum(scanner)
			a, b := solver.Ask(p)
			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!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *