Skip to content
Programming101
Programming101

Learn everything about programming

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

Learn everything about programming

HackerRank Xor sequence problem solution

YASH PAL, 31 July 2024

In this HackerRank Xor sequence problem solution, we have given an array A and the left and right index L, R. we need to determine the XOR sum of the segment of A as A[L] XOR A[L + 1] XOR….A[R – 1] XOR A[R].

HackerRank Xor sequence problem solution

Problem solution in Python.

#!/bin/python3

from math import ceil, floor


Q = int(input().strip())
for a0 in range(Q):
    L,R = input().strip().split(' ')
    L,R = [int(L),int(R)]
    
    val = 0
    
    if R - L + 1 > 4:
        if L%4 == 0:
            val ^= L ^ 1 ^ (L + 3)
            L += 3

        elif L%4 == 1:
            val ^= 1 ^ (L + 2)
            L += 2

        elif L%4 == 2:
            val ^= L + 1
            L += 1
            
        if R%4 == 0:
            val ^= R
            R -= 1
            
        elif R%4 == 1:
            val ^= 1 ^ (R - 1)
            R -= 2
            
        elif R%4 == 2:
            val ^= (R + 1) ^ 1 ^ (R - 2)
            R -= 3
            
        if ((R - L)/4)% 2 == 1:
            val ^= 2
    
    else:    
        for i in range(L, R + 1):
            if i%4 == 1:
                val ^= 1
            elif i%4 == 2:
                val ^= i + 1
            elif i%4 == 0:
                val ^= i
            
    print(val)

Problem solution in Java.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int Q = in.nextInt();
        for(int a0 = 0; a0 < Q; a0++){
            long L = in.nextLong();
            long R = in.nextLong();

            long result = 0;
            long lower = L + (3 - L % 4);
            long upper = R - R % 4;
            if (lower > upper) {
                for (long i = L; i <= R; ++i) result ^= xor(i);
            }
            else {
                for (long i = L; i <= lower; ++i) result ^= xor(i);
                for (long i = R; i >= upper; --i) result ^= xor(i);
            
                long mid = (upper-lower)/4;
                if (mid > 0) result ^= (mid % 2 == 0 ? 0 : 2);                
            }
            System.out.println(result);
        }
    }
    
    static long xor(long i) {
        if (i % 4 == 0) return i;
        else if (i % 4 == 1) return 1;
        else if (i % 4 == 2) return (i+1);
        return 0;
    }
}

Problem solution in C++.

# include <bits/stdc++.h>

# define fi first
# define se second
# define pb push_back


using namespace std;

typedef long long ll;

const int Maxn = 1e5 + 1, md = 1e9 + 7;
const double Eps = 0.01;

using namespace std;

ll n, l , r, a[Maxn];

ll X(ll x)
{
    ll y = x%4;
    if (y == 0)
        return x;
    if (y == 1)
        return 1;
    if (y == 2)
        return x + 1;
    return 0;
}
ll Y(ll x)
{
    ll y = x%8;
    if (x == 0)
        return 0;
    if (y == 0 || y == 1)
        return x;
    if (y == 6 || y == 7)
        return 0;
    if (y == 2 || y == 3)
        return 2;
    return x + 2;
}
int main() {

    cin >> n;

    while (n--)
    {
        cin >> l >> r;
        l = Y(l - 1);
        r = Y(r);
        l = l ^ r;
        cout << l << endl;
    }



}

Problem solution in C.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
long long memoize[4];

unsigned long long arrayfinder(unsigned long long);
int main(){
    int Q;
    scanf("%d",&Q);
    for(int a0 = 0; a0 < Q; a0++){
        unsigned long long L; 
        unsigned long long R;
        unsigned long long total=0;
        scanf("%llu %llu",&L,&R);
        if((R-L)<17){
            for(int i= L;i<=R;i++){
                total^=arrayfinder(i);
            }
        }
        else{
            while(L%8!=7){
                total^=arrayfinder(L);
                L++;
            }
            while(R%8!=7){
                total^=arrayfinder(R);
                R--;
            }
        }
        printf("%llurn",total);
    }
    return 0;
}
unsigned long long arrayfinder(unsigned long long x){
    

       if(x%4==0)return x;
       if(x%4==1)return 1;
       if(x%4==2)return x+1;
       if(x%4==3)return 0;
    
   
    return -1;
}

algorithm coding problems

Post navigation

Previous post
Next post
  • 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
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes