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

13 Jun, 2022
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: Initialize 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.
C++
Java
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the XOR of elements// from the range [l, r]int findXOR(int l, int r){    int ans = 0;    for (int i = l; i <= r; i++) {        ans = ans ^ i;    }    return ans;} // Driver codeint main(){    int l = 4, r = 8;     cout << findXOR(l, r);     return 0;} // this code is contributed by devendra solunke
/*package whatever //do not write package name here */ import java.io.*; class GFG {    // Function to return the XOR of elements    // from the range [l, r]    public static int findXOR(int l, int r)    {        int ans = 0;        for (int i = l; i <= r; i++) {            ans = ans ^ i;        }        return ans;    }     // Driver code    public static void main(String[] args)    {        int l = 4;        int r = 8;         System.out.println(findXOR(l, r));    }}// this code is contributed by devendra solunke
// c# implementation of find xor between given rangeusing System; public class GFG {     // Function to return the XOR of elements    // from the range [l, r]    static int findXOR(int l, int r)    {        int ans = 0;        for (int i = l; i <= r; i++) {            ans = ans ^ i;        }        return ans;    }     // Driver code    static void Main(String[] args)    {        int l = 4;        int r = 8;         Console.WriteLine(findXOR(l, r));    }} // this code is contributed by devendra saunke
// Javascript code to find xor of given range<script>       var l = 4;       var r = 8;       var ans = 0;       var(int i = l; i <= r; i++)       {         ans = ans ^ i;       }     document.write(ans);</script>// this code is contributed by devendra solunke
8
Time complexity: O(N)Auxiliary Space: O(1)
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++
Java
Python3
C#
Javascript
// 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 codeint main(){    int l = 4, r = 8;     cout << findXOR(l, r);     return 0;}
// Java implementation of the approachclass 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
# Python3 implementation of the approachfrom 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 codel = 4; r = 8; print(findXORFun(l, r)); # This code is contributed by PrinciRaj1992
// C# implementation of the approachusing 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
<script>     // Javascript implementation of the approach         // Function to return the XOR of elements    // from the range [1, n]    function findxOR(n)    {        let 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]    function findXOR(l, r)    {        return (findxOR(l - 1) ^ findxOR(r));    }         let l = 4, r = 8;    document.write(findXOR(l, r)); </script>
8
Time Complexity: O(1)Auxiliary Space: O(1)
Rajput-Ji
ankthon
princiraj1992
divyesh072019
adnanirshad158
subhamkumarm348
devendrasalunke
Bitwise-XOR
Arrays
Bit Magic
Mathematical
Arrays
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
How to swap two numbers without using a temporary variable?
Little and Big Endian Mystery
Program to find whether a given number is power of 2