In this HackerRank XORing Ninja problem solution, we have given a list of N space-separated integers and we need to determine and print the XORSUM value. and the XORSUM is equal to the summation of the XOR of the array.

HackerRank XORing Ninja problem solution


Problem solution in Python.

M=1000000007
tests = int(input().strip())
for i in range(0,tests):
    n = int(input().strip())
    a = [int(x.strip()) for x in input().strip().split()]
    b0 = [0 for y in range(0,32)]
    b1 = [0 for y in range(0,32)]
    for k in range(0,n):        
        for j in range(0,32):
            if(a[k] & (1<<j)):
                tmp = b1[j]
                b1[j]=(b1[j]+1+b0[j])%M
                b0[j]=(b0[j]+tmp)%M
            else:
                b1[j]=(b1[j]+b1[j])%M
                b0[j]=(1+b0[j]+b0[j])%M
        

    cum = 0
    for j in range(0,32):
        val = ((1<<j)*b1[j])%M
        cum=(cum+val)%M

    print(cum)


Problem solution in Java.

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

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner s = new Scanner(System.in);
        int numTests = s.nextInt();
        for(int tests = 0; tests < numTests; tests++){
            int n = s.nextInt();
            long sum = 0;
            for(int i = 0; i < n; i++){
                sum = sum|s.nextLong();
            }
            for(int i = 0; i < n-1; i++){
                sum = sum*2 % (1000000007);
            }
            long finalSum = sum << (n-1);
            long finalMod = finalSum % (1000000007);
            System.out.println(sum);
        }
    }
}


Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


const int M = 1000000007;

int p(int x) {
	if (x == 0) {
       return 1;
    }	
  	long long y = p(x >> 1);
  	int r = y * y % M;
  	if (x & 1) {
     	r <<= 1;
    }
  	return (r >= M)?(r - M):r; 
}

int main() {
 int z;
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
  	for (scanf("%d",&z);z;--z) {
      int state = 0,n;
      scanf("%d",&n);
      
    	for (int i = 0; i < n; ++i) {
        	int x = 0;
          	scanf("%d",&x);
        	for (int j = 0; (1 << j) <= x; ++j) {
            	if (x & (1 << j)) {
                  	state |= (1 << j);
                }
              
            }  
        }
      	int r = 0;
        for (int j = 0; j <= 30; ++j) {
          	if ((state & (1 << j)) && ((r += p(n - 1 + j)) >= M)) {
              	r -= M;
            }
          
        }
      	printf("%d\n",r);
    }
    return 0;
}


Problem solution in C.

#include<stdio.h>
#define LL long long int
#define MOD 1000000007

LL power(LL a, LL b)
{
	if (b == 0)
		return 1;
	else
	{
		LL temp=(power(a,b/2))%MOD;
		if(b%2==0)   
			return (temp*temp)%MOD;
		else
			return (((temp*a)%MOD)*temp)%MOD;
	}
}

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		int x=0;
		int i,a;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a);
			x|=a;
		}
		LL ans=power(2,n-1);
		ans=(ans*x)%MOD;
		printf("%lld\n",ans);
	}
	return 0;
}