📌  相关文章
📜  查找数组的前半部分和后半部分元素的XOR

📅  最后修改于: 2021-05-14 08:17:07             🧑  作者: Mango

给定大小为N的数组arr 。任务是找到数组的前一半( N / 2 )个元素和后一半( N – N / 2 )个元素的XOR。
例子:

方法:

  1. 将FirstHalfXOR和SecondHalfXOR初始化为0。
  2. 在FirstHalfXOR中遍历数组和XOR元素,直到当前索引小于N / 2。
  3. 否则,在SecondHalfXOR中使用XOR元素。

下面是上述方法的实现:

C++
// C++ program to find the xor of
// the first half elements and
// second half elements of an array
#include 
using namespace std;
 
// Function to find the xor of
// the first half elements and
// second half elements of an array
void XOROfElements(int arr[], int n){
 
    int FirstHalfXOR = 0;
    int SecondHalfXOR = 0;
 
    for(int i=0; i < n; i++){
         
        // xor of elements in FirstHalfXOR
        if (i < n / 2)
            FirstHalfXOR ^= arr[i];
 
        // xor of elements in SecondHalfXOR
        else
            SecondHalfXOR ^= arr[i];
    }   
    cout << FirstHalfXOR << "," << SecondHalfXOR << endl;
 
}
 
// Driver Code
int main() {
    int arr[] = {20, 30, 50, 10, 55, 15, 42};
 
    int N = sizeof(arr)/sizeof(arr[0]);
 
    // Function call
    XOROfElements(arr, N);
     
    return 0;
}
 
// This code is contributed by AnkitRai01


Java
// Java program to find the xor of
// the first half elements and
// second half elements of an array
 
class GFG{
 
// Function to find the xor of
// the first half elements and
// second half elements of an array
static void XOROfElements(int arr[], int n){
 
    int FirstHalfXOR = 0;
    int SecondHalfXOR = 0;
 
    for(int i = 0; i < n; i++){
         
        // xor of elements in FirstHalfXOR
        if (i < n / 2)
            FirstHalfXOR ^= arr[i];
 
        // xor of elements in SecondHalfXOR
        else
            SecondHalfXOR ^= arr[i];
    }
    System.out.print(FirstHalfXOR + ","
                     + SecondHalfXOR + "\n");
 
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 20, 30, 50, 10, 55, 15, 42 };
 
    int N = arr.length;
 
    // Function call
    XOROfElements(arr, N);
     
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find the xor of
# the first half elements and
# second half elements of an array
 
# Function to find the xor of
# the first half elements and
# second half elements of an array
def XOROfElements(arr, n):
 
    FirstHalfXOR = 0;
    SecondHalfXOR = 0;
 
    for i in range(n):
         
        # xor of elements in FirstHalfXOR
        if (i < n // 2):
            FirstHalfXOR ^= arr[i];
 
        # xor of elements in SecondHalfXOR
        else:
            SecondHalfXOR  ^= arr[i];
             
    print(FirstHalfXOR,",",SecondHalfXOR);
 
# Driver Code
arr = [20, 30, 50, 10, 55, 15, 42];
 
N = len(arr);
 
# Function call
XOROfElements(arr, N);


C#
// C# program to find the xor of
// the first half elements and
// second half elements of an array
using System;
 
class GFG{
 
// Function to find the xor of
// the first half elements and
// second half elements of an array
static void XOROfElements(int []arr, int n)
{
    int FirstHalfXOR = 0;
    int SecondHalfXOR = 0;
 
    for(int i = 0; i < n; i++)
    {
        
       // xor of elements in FirstHalfXOR
       if (i < n / 2)
           FirstHalfXOR ^= arr[i];
            
       // xor of elements in SecondHalfXOR
       else
           SecondHalfXOR ^= arr[i];
    }
     
    Console.Write(FirstHalfXOR + "," +
                  SecondHalfXOR + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 20, 30, 50, 10, 55, 15, 42 };
    int N = arr.Length;
 
    // Function call
    XOROfElements(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
56, 24

时间复杂度: O(N)