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

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.

#!/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;
}

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