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:
- Initialize FirstHalfXOR and SecondHalfXOR as 0.
- Traverse the array and XOR elements in FirstHalfXOR until the current index is less than N/2.
- 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 arrayvoid 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 Codeint 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 arrayclass GFG{// Function to find the xor of// the first half elements and// second half elements of an arraystatic 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 Codepublic 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 arraydef 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 Codearr = [20, 30, 50, 10, 55, 15, 42];N = len(arr);# Function callXOROfElements(arr, N); |
C#
// C# program to find the xor of// the first half elements and// second half elements of an arrayusing System;class GFG{// Function to find the xor of// the first half elements and// second half elements of an arraystatic 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 Codepublic 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> |
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA


