Find the XOR of first half and second half elements of an array

Given an array arr of size N. The task is to find the XOR of the first half (N/2) elements and second half elements (N – N/2) of an array.

Examples:

Input: arr[]={20, 30, 50, 10, 55, 15, 42}
Output: 56, 24
Explanation:
XOR of first half elements 20 ^ 30 ^ 50 is 56
Xor of second half elements 10 ^ 55 ^ 15 ^ 42 is 24

Input: arr[]={50, 45, 36, 6, 8, 87}
Output: 59, 89

Approach:



  1. Initialize FirstHalfXOR and SecondHalfXOR as 0.
  2. Traverse the array and XOR elements in FirstHalfXOR until the current index is less than N/2.
  3. Otherwise, XOR elements in SecondHalfXOR.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the xor of 
// the first half elements and 
// second half elements of an array 
#include <iostream>
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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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); 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

56, 24

Time complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, 29AjayKumar