Skip to content
Related Articles

Related Articles

Count even and odd Bitwise XORs of consecutive numbers in a range [L, R] starting from L
  • Last Updated : 05 Apr, 2021

Given two integers L and R, the task is to find the count of even and odd Bitwise XOR values of consecutive numbers from the range [L, R] starting from L.

Examples:

Input: L = 2, R = 7
Output: Even = 3, Odd = 3
Explanation: Taking bitwise XOR of continuous numbers:
2
2 ^ 3 = 1
2 ^ 3 ^ 4 = 5
2 ^ 3 ^ 4 ^ 5 = 0
2 ^ 3 ^ 4 ^ 5 ^ 6 = 6
2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 = 1
Therefore, Bitwise XOR values obtained are {2, 1, 5, 0, 6, 1}.
Therefore, count of even XOR values is 3 and odd XOR values is 3.

Input: L = 1, R = 7
Output: Even = 3, Odd = 4

Naive Approach: The simplest approach is to traverse all numbers in the range [L, R] and perform Bitwise XOR of consecutive numbers starting from L. Finally, count the number of even and odd values Bitwise XOR values obtained. 
Time Complexity: O(R – L)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is based on the following observation that the Bitwise XOR from 1 to N can be calculated in constant time:



  • Find the remainder of N when divide by 4.
  • If remainder obtained is 0, then XOR will be equal to N.
  • If remainder obtained is 1, then XOR will be equal to 1.
  • If remainder obtained is 2, then XOR will be equal to N + 1.
  • If remainder obtained is 3, then XOR obtained will be 0.

From the above observation, it can be concluded that the even XOR values are either 0 or multiples of 4. Follow the steps below to solve the problem:

  • Store the number of elements in the range [L, R] in a variable, say X.
  • Store the count of even XOR values by dividing X by 4 and multiplying by 2 in a variable, say Even.
  • If L is odd and X % 4 is equal to 3, then increment Even by 1.
  • Otherwise, if L is even and X % 4 is greater than 0, then increment Even by 1.
  • Store the count of odd XOR values in a variable Odd = X – Even.
  • After completing the above steps, print the value of Even and Odd as the result.

Below is the implementation of the above approach.

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Print count of even and odd numbers
// of XOR value from L to R
void countEvenOdd(int L, int R)
{
    // Store the number of elements
    // between L and R
    int range = R - L + 1;
 
    // Count of even XOR values
    int even = (range / 4) * 2;
 
    // If L is odd and range % 4 = 3
    if ((L & 1) && (range % 4 == 3)) {
 
        // Increment even by 1
        even++;
    }
 
    // If L is even and range % 4 !=0
    else if (!(L & 1) && (range % 4)) {
 
        // Increment even by 1
        even++;
    }
 
    // Print the answer
    cout << "Even = " << even
         << ", Odd = " << range - even;
}
 
// Driver Code
int main()
{
    int L = 2, R = 7;
    countEvenOdd(L, R);
 
    return 0;
}

Java




// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG
{
  // Print count of even and odd numbers
  // of XOR value from L to R
  static void countEvenOdd(int L, int R)
  {
 
    // Store the number of elements
    // between L and R
    int range = R - L + 1;
 
    // Count of even XOR values
    int even = (range / 4) * 2;
 
    // If L is odd and range % 4 = 3
    if ((L & 1) != 0 && (range % 4 == 3))
    {
 
      // Increment even by 1
      even++;
    }
 
    // If L is even and range % 4 !=0
    else if ((L & 1) == 0 && (range % 4 != 0))
    {
 
      // Increment even by 1
      even++;
    }
 
    // Print the answer
    System.out.print("Even = " + even +
                     ", Odd = " + (range - even));
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int L = 2, R = 7;
    countEvenOdd(L, R);
  }
}
 
// This code is contributed by sanjoy_62.

Python3




# Python program for the above approach
 
# Prcount of even and odd numbers
# of XOR value from L to R
def countEvenOdd(L, R):
   
    # Store the number of elements
    # between L and R
    range = R - L + 1;
 
    # Count of even XOR values
    even = (range // 4) * 2;
 
    # If L is odd and range % 4 = 3
    if ((L & 1) != 0 and (range % 4 == 3)):
 
        # Increment even by 1
        even += 1;
 
    # If L is even and range % 4 !=0
    elif ((L & 1) == 0 and (range % 4 != 0)):
 
        # Increment even by 1
        even += 1;
 
    # Prthe answer
    print("Even = " , even ,\
          ", Odd = " , (range - even));
 
# Driver Code
if __name__ == '__main__':
    L = 2; R = 7;
    countEvenOdd(L, R);
 
# This code is contributed by shikhasingrajput

C#




// C# program for the above approach
using System;
class GFG
{
     
    // Print count of even and odd numbers
    // of XOR value from L to R
    static void countEvenOdd(int L, int R)
    {
       
        // Store the number of elements
        // between L and R
        int range = R - L + 1;
       
        // Count of even XOR values
        int even = (range / 4) * 2;
       
        // If L is odd and range % 4 = 3
        if ((L & 1) != 0 && (range % 4 == 3))
        {
       
            // Increment even by 1
            even++;
        }
       
        // If L is even and range % 4 !=0
        else if ((L & 1) == 0 && (range % 4 != 0))
        {
       
            // Increment even by 1
            even++;
        }
       
        // Print the answer
        Console.Write("Even = " + even +
                      ", Odd = " + (range - even));
    }
 
  // Driver code
  static void Main()
  {
    int L = 2, R = 7;
    countEvenOdd(L, R);
  }
}
 
// This code is contributed by divyeshrabadiya07.

Javascript




<script>
 
// Javascript program for the above approach
 
// Print count of even and odd numbers
// of XOR value from L to R
function countEvenOdd(L, R)
{
    // Store the number of elements
    // between L and R
    let range = R - L + 1;
 
    // Count of even XOR values
    let even = parseInt(range / 4) * 2;
 
    // If L is odd and range % 4 = 3
    if ((L & 1) && (range % 4 == 3)) {
 
        // Increment even by 1
        even++;
    }
 
    // If L is even and range % 4 !=0
    else if (!(L & 1) && (range % 4)) {
 
        // Increment even by 1
        even++;
    }
 
    // Print the answer
    document.write("Even = " + even
         + ", Odd = " + (range - even));
}
 
// Driver Code
let L = 2, R = 7;
countEvenOdd(L, R);
 
</script>
Output: 
Even = 3, Odd = 3

 

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :