Find XOR of numbers from the range [L, R]

Given two integers L and R, the task is to find the XOR of elements of the range [L, R].

Examples :

Input: L = 4, R = 8
Output: 8
4 ^ 5 ^ 6 ^ 7 ^ 8 = 8



Input: L = 3, R = 7
Output: 3

Naive Approach: Intilialize answer as zero, Traverse all numbers from L to R and perform XOR of the numbers one by one with the answer. This would take O(N) time.

Efficient Approach: By following the approach discussed here, we can find the XOR of elements from the range [1, N] in O(1) time.
Using this approach, we have to find xor of elements from the range [1, L – 1] and from the range [1, R] and then xor the respective answers again to get the xor of the elements from the range [L, R]. This is because every element from the range [1, L – 1] will get XORed twice in the result resulting in a 0 which when XORed with the elements of the range [L, R] will give the result.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the XOR of elements
// from the range [1, n]
int findXOR(int n)
{
    int mod = n % 4;
  
    // If n is a multiple of 4
    if (mod == 0)
        return n;
  
    // If n % 4 gives remainder 1
    else if (mod == 1)
        return 1;
  
    // If n % 4 gives remainder 2
    else if (mod == 2)
        return n + 1;
  
    // If n % 4 gives remainder 3
    else if (mod == 3)
        return 0;
}
  
// Function to return the XOR of elements
// from the range [l, r]
int findXOR(int l, int r)
{
    return (findXOR(l - 1) ^ findXOR(r));
}
  
// Driver code
int main()
{
    int l = 4, r = 8;
  
    cout << findXOR(l, r);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
    // Function to return the XOR of elements
    // from the range [1, n]
    static int findXOR(int n)
    {
        int mod = n % 4;
  
        // If n is a multiple of 4
        if (mod == 0)
            return n;
  
        // If n % 4 gives remainder 1
        else if (mod == 1)
            return 1;
  
        // If n % 4 gives remainder 2
        else if (mod == 2)
            return n + 1;
  
        // If n % 4 gives remainder 3
        else if (mod == 3)
            return 0;
        return 0;
    }
  
    // Function to return the XOR of elements
    // from the range [l, r]
    static int findXOR(int l, int r)
    {
        return (findXOR(l - 1) ^ findXOR(r));
    }
  
    // Driver code
    public static void main(String[] args) 
    {
  
        int l = 4, r = 8;
  
            System.out.println(findXOR(l, r));
    }
}
  
// This code contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
from operator import xor
  
# Function to return the XOR of elements
# from the range [1, n]
def findXOR(n):
    mod = n % 4;
  
    # If n is a multiple of 4
    if (mod == 0):
        return n;
  
    # If n % 4 gives remainder 1
    elif (mod == 1):
        return 1;
  
    # If n % 4 gives remainder 2
    elif (mod == 2):
        return n + 1;
  
    # If n % 4 gives remainder 3
    elif (mod == 3):
        return 0;
  
# Function to return the XOR of elements
# from the range [l, r]
def findXORFun(l, r):
    return (xor(findXOR(l - 1) , findXOR(r)));
  
# Driver code
l = 4; r = 8;
  
print(findXORFun(l, r));
  
# This code is contributed by PrinciRaj1992

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG 
    // Function to return the XOR of elements 
    // from the range [1, n] 
    static int findXOR(int n) 
    
        int mod = n % 4; 
  
        // If n is a multiple of 4 
        if (mod == 0) 
            return n; 
  
        // If n % 4 gives remainder 1 
        else if (mod == 1) 
            return 1; 
  
        // If n % 4 gives remainder 2 
        else if (mod == 2) 
            return n + 1; 
  
        // If n % 4 gives remainder 3 
        else if (mod == 3) 
            return 0; 
        return 0; 
    
  
    // Function to return the XOR of elements 
    // from the range [l, r] 
    static int findXOR(int l, int r) 
    
        return (findXOR(l - 1) ^ findXOR(r)); 
    
  
    // Driver code 
    public static void Main() 
    
  
        int l = 4, r = 8; 
  
            Console.WriteLine(findXOR(l, r)); 
    
  
// This code is contributed by AnkitRai01

chevron_right


Output:

8


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.