📜  给定范围内所有偶数的XOR

📅  最后修改于: 2021-04-22 00:21:39             🧑  作者: Mango

给定两个整数LR ,任务是计算范围[L,R]中所有偶数的按位XOR。

例子:

天真的方法:解决问题的最简单方法是遍历[L,R]范围内的所有偶数并打印所有偶数的按位XOR。

时间复杂度: O(R – L)
辅助空间: O(1)

高效的方法:可以基于以下观察来优化上述方法:

请按照以下步骤解决问题:

  • 从范围[1,(R)/ 2]中查找所有数字的按位XOR并将其存储在变量xor_r中
  • 在[1,(L – 1)/ 2]范围内找到所有数字的按位XOR,并将其存储在变量xor_l中。
  • 最后,打印xor_r ^ xor_l的值。

下面是上述方法的实现:

C++
// C++ Implementation of the above approach
#include 
using namespace std;
 
// Function to calculate XOR of
// numbers in the range [1, n]
int bitwiseXorRange(int n)
{
 
    // If n is divisible by 4
    if (n % 4 == 0)
        return n;
 
    // If n mod 4 is equal to 1
    if (n % 4 == 1)
        return 1;
 
    // If n mod 4 is equal to 2
    if (n % 4 == 2)
        return n + 1;
 
    return 0;
}
 
// Function to find XOR of even
// numbers in the range [l, r]
int evenXorRange(int l, int r)
{
 
    // Stores XOR of even numbers
    // in the range [1, l - 1]
    int xor_l;
 
    // Stores XOR of even numbers
    // in the range [1, r]
    int xor_r;
 
    // Update xor_r
    xor_r
        = 2 * bitwiseXorRange(r / 2);
 
    // Update xor_l
    xor_l
        = 2 * bitwiseXorRange((l - 1) / 2);
 
    return xor_l ^ xor_r;
}
 
// Driver Code
int main()
{
    int l = 10;
    int r = 20;
    cout << evenXorRange(l, r);
 
    return 0;
}


Java
// Java Implementation of the above approach
class GFG
{
     
  // Function to calculate XOR of
  // numbers in the range [1, n]
  static int bitwiseXorRange(int n)
  {
 
    // If n is divisible by 4
    if (n % 4 == 0)
      return n;
 
    // If n mod 4 is equal to 1
    if (n % 4 == 1)
      return 1;
 
    // If n mod 4 is equal to 2
    if (n % 4 == 2)
      return n + 1;
 
    return 0;
  }
 
  // Function to find XOR of even
  // numbers in the range [l, r]
  static int evenXorRange(int l, int r)
  {
 
    // Stores XOR of even numbers
    // in the range [1, l - 1]
    int xor_l;
 
    // Stores XOR of even numbers
    // in the range [1, r]
    int xor_r;
 
    // Update xor_r
    xor_r
      = 2 * bitwiseXorRange(r / 2);
 
    // Update xor_l
    xor_l
      = 2 * bitwiseXorRange((l - 1) / 2);
 
    return xor_l ^ xor_r;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    int l = 10;
    int r = 20;
    System.out.print(evenXorRange(l, r));   
  }
}
 
// This code is contributed by AnkThon


Python3
# Python3 implementation of the above approach
 
# Function to calculate XOR of
# numbers in the range [1, n]
def bitwiseXorRange(n):
 
    # If n is divisible by 4
    if (n % 4 == 0):
        return n
 
    # If n mod 4 is equal to 1
    if (n % 4 == 1):
        return 1
 
    # If n mod 4 is equal to 2
    if (n % 4 == 2):
        return n + 1
 
    return 0
 
# Function to find XOR of even
# numbers in the range [l, r]
def evenXorRange(l, r):
 
    # Stores XOR of even numbers
    # in the range [1, l - 1]
    #xor_l
 
    # Stores XOR of even numbers
    # in the range [1, r]
    #xor_r
 
    # Update xor_r
    xor_r = 2 * bitwiseXorRange(r // 2)
 
    # Update xor_l
    xor_l = 2 * bitwiseXorRange((l - 1) // 2)
 
    return xor_l ^ xor_r
 
# Driver Code
if __name__ == '__main__':
     
    l = 10
    r = 20
     
    print(evenXorRange(l, r))
 
# This code is contributed by mohit kumar 29


C#
// C# Implementation of the above approach
using System;
class GFG {
     
  // Function to calculate XOR of
  // numbers in the range [1, n]
  static int bitwiseXorRange(int n)
  {
  
    // If n is divisible by 4
    if (n % 4 == 0)
      return n;
  
    // If n mod 4 is equal to 1
    if (n % 4 == 1)
      return 1;
  
    // If n mod 4 is equal to 2
    if (n % 4 == 2)
      return n + 1;
  
    return 0;
  }
  
  // Function to find XOR of even
  // numbers in the range [l, r]
  static int evenXorRange(int l, int r)
  {
  
    // Stores XOR of even numbers
    // in the range [1, l - 1]
    int xor_l;
  
    // Stores XOR of even numbers
    // in the range [1, r]
    int xor_r;
  
    // Update xor_r
    xor_r
      = 2 * bitwiseXorRange(r / 2);
  
    // Update xor_l
    xor_l
      = 2 * bitwiseXorRange((l - 1) / 2);
  
    return xor_l ^ xor_r;
  }
   
  // Driver code
  static void Main()
  {
    int l = 10;
    int r = 20;
    Console.Write(evenXorRange(l, r));   
  }
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
30

时间复杂度: O(1)。
辅助空间: O(1)