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 24Input: 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 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 |
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.
Recommended Posts:
- Find the sum of the first half and second half elements of an array
- Find prime numbers in the first half and second half of an array
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Reorder an array such that sum of left half is not equal to sum of right half
- Find elements larger than half of the elements in an array
- Sort first half in ascending and second half in descending order | Set 2
- Sort first half in ascending and second half in descending order | 1
- Count number of rotated strings which have more number of vowels in the first half than second half
- Find if array has an element whose value is half of array sum
- Maximum sum such that exactly half of the elements are selected and no two adjacent
- Reduce every element of the array to it's half retaining the sum zero
- Check if at least half array is reducible to zero by performing some operations
- Minimum insertions to make XOR of an Array equal to half of its sum
- Find all even length binary sequences with same sum of first and second half bits | Iterative
- Check if minimum element in array is less than or equals half of every other element
- Number of subarrays such that XOR of one half is equal to the other
- Check if the frequency of any character is more than half the length of the string
- Program to print half Diamond star pattern
- Find original array from encrypted array (An array of sums of other elements)
- Find Kth element in an array containing odd elements first and then even elements
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.

