📜  给定范围内所有奇数的按位XOR

📅  最后修改于: 2021-04-27 09:14:21             🧑  作者: Mango

给定一个整数N ,任务是找到范围[1,N]中所有奇数的按位XOR。

例子:

天真的方法:解决问题的最简单方法是在[1,N]范围内进行迭代并针对每个值检查其是否为奇数。计算找到的每个奇数元素的按位XOR,并将其打印为所需结果。
时间复杂度: O(N)
辅助空间: O(1)

高效方法:为了优化上述方法,我们的想法是使用以下公式计算所有小于或等于N的奇数的按位XOR

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

  • 将范围为[1,N]的数字的XOR存储在变量中,例如K。
  • 如果N为奇数,则将[X,(N – 1)/ 2]范围内的数字的XOR存储在变量g中。否则,以g为单位存储数字[X,N / 2]的XOR。
  • 使用导出的公式,将结果更新为K ^(2&g)
  • 完成上述步骤后,打印结果值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate Bitwise
// XOR of odd numbers in the range [1, N]
int findXOR(int n)
{
    // N & 3 is equivalent to n % 4
    switch (n & 3) {
 
    // If n is multiple of 4
    case 0:
        return n;
 
    // If n % 4 gives remainder 1
    case 1:
        return 1;
 
    // If n % 4 gives remainder 2
    case 2:
        return n + 1;
 
    // If n % 4 gives remainder 3
    case 3:
        return 0;
    }
}
 
// Function to find the XOR of odd
// numbers less than or equal to N
void findOddXOR(int n)
{
 
    // If number is even
    if (n % 2 == 0)
 
        // Print the answer
        cout << ((findXOR(n))
                 ^ (2 * findXOR(n / 2)));
 
    // If number is odd
    else
 
        // Print the answer
        cout << ((findXOR(n))
                 ^ (2 * findXOR((n - 1) / 2)));
}
 
// Driver Code
int main()
{
    int N = 11;
 
    // Function Call
    findOddXOR(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to calculate Bitwise
  // XOR of odd numbers in the range [1, N]
  static int findXOR(int n)
  {
 
    // N & 3 is equivalent to n % 4
    switch (n & 3)
    {
 
        // If n is multiple of 4
      case 0:
        return n;
 
        // If n % 4 gives remainder 1
      case 1:
        return 1;
 
        // If n % 4 gives remainder 2
      case 2:
        return n + 1;
    }
    // If n % 4 gives remainder 3
    return 0;
 
  }
 
  // Function to find the XOR of odd
  // numbers less than or equal to N
  static void findOddXOR(int n)
  {
 
    // If number is even
    if (n % 2 == 0)
 
      // Print the answer
      System.out.print(((findXOR(n))
                        ^ (2 * findXOR(n / 2))));
 
    // If number is odd
    else
 
      // Print the answer
      System.out.print(((findXOR(n))
                        ^ (2 * findXOR((n - 1) / 2))));
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 11;
 
    // Function Call
    findOddXOR(N);
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python program for the above approach
 
# Function to calculate Bitwise
# XOR of odd numbers in the range [1, N]
def findXOR(n):
   
    # N & 3 is equivalent to n % 4
    if (n % 4 == 0):
       
        # If n is multiple of 4
        return n;
    elif (n % 4 == 1):
       
        # If n % 4 gives remainder 1
        return 1;
 
    # If n % 4 gives remainder 2
    elif (n % 4 == 2):
        return n + 1;
 
    # If n % 4 gives remainder 3
    elif (n % 4 == 3):
        return 0;
 
# Function to find the XOR of odd
# numbers less than or equal to N
def findOddXOR(n):
   
    # If number is even
    if (n % 2 == 0):
 
        # Prthe answer
        print(((findXOR(n)) ^ (2 * findXOR(n // 2))));
 
    # If number is odd
    else:
 
        # Prthe answer
        print(((findXOR(n)) ^ (2 * findXOR((n - 1) // 2))));
 
# Driver Code
if __name__ == '__main__':
    N = 11;
 
    # Function Call
    findOddXOR(N);
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
public class GFG
{
 
  // Function to calculate Bitwise
  // XOR of odd numbers in the range [1, N]
  static int findXOR(int n)
  {
 
    // N & 3 is equivalent to n % 4
    switch (n & 3)
    {
 
        // If n is multiple of 4
      case 0:
        return n;
 
        // If n % 4 gives remainder 1
      case 1:
        return 1;
 
        // If n % 4 gives remainder 2
      case 2:
        return n + 1;
    }
    // If n % 4 gives remainder 3
    return 0;
 
  }
 
  // Function to find the XOR of odd
  // numbers less than or equal to N
  static void findOddXOR(int n)
  {
 
    // If number is even
    if (n % 2 == 0)
 
      // Print the answer
      Console.Write(((findXOR(n))
                     ^ (2 * findXOR(n / 2))));
 
    // If number is odd
    else
 
      // Print the answer
      Console.Write(((findXOR(n))
                     ^ (2 * findXOR((n - 1) / 2))));
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int N = 11;
 
    // Function Call
    findOddXOR(N);
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
2

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