Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIFind the XOR of first half and second half elements of an...

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




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


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




<script>
 
// Javascript 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
    function XOROfElements(arr , n)
    {
 
        var FirstHalfXOR = 0;
        var SecondHalfXOR = 0;
 
        for (i = 0; i < n; i++) {
 
            // xor of elements in FirstHalfXOR
            if (i < parseInt(n / 2))
                FirstHalfXOR ^= arr[i];
 
            // xor of elements in SecondHalfXOR
            else
                SecondHalfXOR ^= arr[i];
        }
        document.write(
        FirstHalfXOR + ", " + SecondHalfXOR + "\n"
        );
 
    }
 
    // Driver Code
     
        var arr = [ 20, 30, 50, 10, 55, 15, 42 ];
 
        var N = arr.length;
 
        // Function call
        XOROfElements(arr, N);
 
 
// This code contributed by umadevi9616
 
</script>


Output: 

56, 24

 

Time complexity: O(N), as we are using a loop to traverse the array.
Auxiliary Space: O(1), as we are not using any extra space.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments