Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank XORing Ninja problem solution

YASH PAL, 31 July 2024

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

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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("%dn",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("%lldn",ans);
	}
	return 0;
}

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes