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

📅  最后修改于: 2021-09-06 11:31:04             🧑  作者: Mango

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

方法:

  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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live