Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

War of XORs CodeChef Solution

Problem -War of XORs 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.
<<Previous CodeChef Problem Next Codechef Problem>>

War of XORs CodeChef Solution in C++14

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

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int i, arr[n], freq[1000010] = {0};
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
            freq[arr[i]]++;
        }
        ll odd = 0, even = 0, ans = 0;
        for (int i = 0; i < n; i++)
        {
            if (arr[i] & 1)
                odd++;
            else
                even++;
        }

        for (int i = 0; i < n; i++)
        {
            if (arr[i] & 1)
                ans += odd;
            else
                ans += even;
            ans -= freq[arr[i] ^ 2];
            ans -= freq[arr[i]];
        }
        cout << (ans >> 1) << endl;
    }

    return 0;
}

War of XORs CodeChef Solution in PYTH 3

# cook your dish here
from collections import defaultdict
for _ in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    d=defaultdict(lambda:0)
    even=odd=0
    count=0
    for i in l:
        d[i]+=1
        if i&1:odd+=1
        else:even+=1
    for i in l:
        if(i%2 == 0):
            count = count + even
        else:
            count = count + odd
        if(i ^ 2 in d):
            count = count - d[i^2]
        count = count - d[i]
    print(int(count/2))

War of XORs CodeChef Solution in C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define int long long int
#define li long int
#define inf 1000010
#define mod 998244353

int Floor(int a, int b)
{
    if(a%b == 0)
    {
        return (a/b);
    }
    if(a>=0&&b>=0 || a<=0&&b<=0)
    {
        return a/b;
    }
    else
    {
        return (a/b - 1);
    }
}

int max(int a, int b)
{
    int m = a;
    if(b>a)
        m = b;
    return m;
}

int min(int a, int b)
{
    int m = a;
    if(b<a)
        m = b;
    return m;
}

// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;
 
    /* create temp arrays */
    int L[n1], R[n2];
 
    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];
 
    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        }
        else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
 
    /* Copy the remaining elements of L[], if there
    are any */
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
 
    /* Copy the remaining elements of R[], if there
    are any */
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}
 
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
    if (l < r) {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l + (r - l) / 2;
 
        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);
 
        merge(arr, l, m, r);
    }
}

int H[inf];

void init()
{
    int i;
    for ( i = 0; i < inf; i++)
    {
        H[i] = 0;
    }
    return;
}

void close(int D[], int lend)
{
    for (int i = 0; i < lend; i++)
    {
        H[D[i]] = 0;
    }
    return;
}

void program()
{
    int n;
    scanf("%lld", &n);
    int A[n];
    int D[n];
    int i;
    for ( i = 0; i < n; i++)
    {
        scanf("%lld", &A[i]);
    }
    //only even numbers >=4  can be sum of same parity prime numbers
    int j = 0;
    for ( i = 0; i < n; i++)
    {
        if(H[A[i]] == 0)
        {
            D[j] = A[i];
            H[A[i]]++;
            j++;
        }
        else
        {
            H[A[i]]++;
        }
    }
    int len_d = j;
    int even = 0, odd = 0;
    for ( i = 0; i < n; i++)
    {
        if(A[i]%2 == 0)
        {
            even++;
        }
        else
        {
            odd++;
        }
    }
    /*for ( i = 0; i < len_d; i++)
    {
        printf("%d %d\n", D[i], H[D[i]]);
    }
    printf("%d %d\n", even, odd);*/
    int ans = (even)*(even-1) + (odd)*(odd-1);
    for ( i = 0; i < len_d; i++)
    {
        int b = (D[i])^2;
        ans -= H[b]*H[D[i]];
        ans -= (H[D[i]])*(H[D[i]]-1);
    }
    printf("%lld\n", ans/2);
    close(D, len_d);
}

int main(void) 
{
    init();
    int T;
    scanf("%lld", &T);
    //printf("%d\n", T);
    // T = 1;
    for (int i = 0; i < T; i++)
    {
        program();
    }
	return 0;
}

War of XORs 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
	{
		InputReader sc = new InputReader(System.in);
		int t = sc.nextInt();
		while (t-- > 0){
		    int n = sc.nextInt();
		    long f[] = new long[1000011];
		    long one = 0;
		    long res = 0;
		    for (int i = 0 ; i < n ; i++){
		        long temp = sc.nextInt();
		        res = res - f[(int)temp] - f[(int)temp^2];
		        if ((temp & 1l) == 1l) res = res + one++;
		        else res = res + ((long)i - one);
		        f[(int)temp]++;
		    }
		    System.out.println(res);
		}
	}
	
	static class InputReader{
		BufferedReader br;
		StringTokenizer st;
        
        public InputReader(InputStream in){
        	try {
        		br = new BufferedReader(new
        				InputStreamReader(in)); 
        	}catch (Exception e) {
        		System.out.println(e.toString());
        	}
        }
        
        String next(){
        	while (st == null || !st.hasMoreElements()){
        		try{
        			st = new StringTokenizer(br.readLine()); 
                }catch (IOException  e){
                	e.printStackTrace(); 
                }
            }
            return st.nextToken(); 
        }
        
        int nextInt(){
        	return Integer.parseInt(next()); 
        }
        
        long nextLong(){
        	return Long.parseLong(next()); 
        }
        
        double nextDouble(){
        	return Double.parseDouble(next()); 
        }
        
        String nextLine(){
        	String str = "";
        	try{
        		str = br.readLine();
        	}catch (IOException e){
        		e.printStackTrace(); 
            }
            return str; 
        }
    }
}

War of XORs CodeChef Solution in PYPY 3

from collections import defaultdict
for _ in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    d=defaultdict(lambda:0)
    even=odd=0
    count=0
    for i in l:
        d[i]+=1
        if i&1:odd+=1
        else:even+=1
    for i in l:
        if(i%2 == 0):
            count = count + even
        else:
            count = count + odd
        if(i ^ 2 in d):
            count = count - d[i^2]
        count = count - d[i]
    print(int(count/2))

War of XORs CodeChef Solution in PYTH

MX = 10**6
t = int(raw_input())
for i in range(t):
	N = int(raw_input())
	st = raw_input().split()
	F = [0 for x in range(MX+1)]
	ne = 0
	for x in st:
		n = int(x)
		if n%2 == 0:
			ne += 1
		# endif
		F[n] += 1
	# endfor x
	no = N-ne
	tot = no*(no-1)/2 + ne*(ne-1)/2
	for n in range(1,MX+1):
		m = F[n]
		if m > 0:
			tot -= m*(m-1)/2
			if n&2 > 0:
				k = F[n-2]
				tot -= m*k
			# endif
		# endif
	# endfor x
	print tot
# endfor i

War of XORs CodeChef Solution in C#

using System;
using System.Collections.Generic;

namespace ChallengeSolver.CodeChef
{
    /// <summary>
    /// https://www.codechef.com/SEPT18B/problems/XORIER
    /// </summary>
    static class Xorier
    {
        public static void Main()
        {
            int testCount = int.Parse(Console.ReadLine());
            for (int t = 0; t < testCount; t++)
            {
                Console.ReadLine();
                string[] rawInput = Console.ReadLine().Split(' ');
                long evenCount = 0;
                long oddCount = 0;
                Dictionary<int, long> evenSet = new Dictionary<int, long>();
                Dictionary<int, long> oddSet = new Dictionary<int, long>();
                for (int i = 0; i < rawInput.Length; i++)
                {
                    int value = int.Parse(rawInput[i]);
                    if ((value & 1) == 0)
                    {
                        oddCount++;
                        if (!evenSet.ContainsKey(value))
                        {
                            evenSet.Add(value, 1);
                        }
                        else
                        {
                            evenSet[value]++;
                        }
                    }
                    else
                    {
                        evenCount++;
                        if (!oddSet.ContainsKey(value))
                        {
                            oddSet.Add(value, 1);
                        }
                        else
                        {
                            oddSet[value]++;
                        }
                    }
                }

                long result = evenCount * (evenCount - 1) / 2 + oddCount * (oddCount - 1) / 2;
                long componentCount = 0;
                foreach (var i in evenSet)
                {
                    result -= i.Value * (i.Value - 1) / 2;
                    int complement = i.Key ^ 2;
                    if (evenSet.ContainsKey(complement))
                    {
                        componentCount += (i.Value * evenSet[complement]);
                    }
                }
                result -= componentCount / 2;
                componentCount = 0;
                foreach (var i in oddSet)
                {
                    result -= i.Value * (i.Value - 1) / 2;
                    int complement = i.Key ^ 2;
                    if (oddSet.ContainsKey(complement))
                    {
                        componentCount += (i.Value * oddSet[complement]);
                    }
                }
                result -= componentCount / 2;
                Console.WriteLine(result);
            }
        }
    }
}
War of XORs CodeChef Solution Review:

In our experience, we suggest you solve this War of XORs 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 War of XORs CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this War of XORs 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 *