Check if the array has an element which is equal to XOR of remaining elements
Last Updated :
05 Sep, 2022
Given an array arr[] of N elements, the task is to check if the array has an element which is equal to the XOR of all the remaining elements.
Examples:
Input: arr[] = { 8, 2, 4, 15, 1 }
Output: Yes
8 is the required element as 2 ^ 4 ^ 15 ^ 1 = 8.
Input: arr[] = {4, 2, 3}
Output: No
Approach: First, take the XOR of all the elements of the array and store it in a variable xorArr. Now traverse the complete array again and for every element, calculate the xor of the array elements excluding the current element arr[i] i.e. x = xorArr ^ arr[i].
If x = arr[i] then arr[i] is the required element and hence print Yes. If no such element is found then print No.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function that returns true if the array
// contains an element which is equal to
// the XOR of the remaining elements
bool containsElement(int arr[], int n)
{
// To store the XOR of all
// the array elements
int xorArr = 0;
for (int i = 0; i < n; ++i)
xorArr ^= arr[i];
// For every element of the array
for (int i = 0; i < n; ++i) {
// Take the XOR after excluding
// the current element
int x = xorArr ^ arr[i];
// If the XOR of the remaining elements
// is equal to the current element
if (arr[i] == x)
return true;
}
// If no such element is found
return false;
}
// Driver code
int main()
{
int arr[] = { 8, 2, 4, 15, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
if (containsElement(arr, n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function that returns true if the array
// contains an element which is equal to
// the XOR of the remaining elements
static boolean containsElement(int [] arr, int n)
{
// To store the XOR of all
// the array elements
int xorArr = 0;
for (int i = 0; i < n; ++i)
xorArr ^= arr[i];
// For every element of the array
for (int i = 0; i < n; ++i)
{
// Take the XOR after excluding
// the current element
int x = xorArr ^ arr[i];
// If the XOR of the remaining elements
// is equal to the current element
if (arr[i] == x)
return true;
}
// If no such element is found
return false;
}
// Driver code
public static void main (String[] args)
{
int [] arr = { 8, 2, 4, 15, 1 };
int n = arr.length;
if (containsElement(arr, n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by ihritik
Python3
# Python3 implementation of the approach
# Function that returns true if the array
# contains an element which is equal to
# the XOR of the remaining elements
def containsElement(arr, n):
# To store the XOR of all
# the array elements
xorArr = 0
for i in range(n):
xorArr ^= arr[i]
# For every element of the array
for i in range(n):
# Take the XOR after excluding
# the current element
x = xorArr ^ arr[i]
# If the XOR of the remaining elements
# is equal to the current element
if (arr[i] == x):
return True
# If no such element is found
return False
# Driver Code
arr = [8, 2, 4, 15, 1]
n = len(arr)
if (containsElement(arr, n)):
print("Yes")
else:
print("No")
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function that returns true if the array
// contains an element which is equal to
// the XOR of the remaining elements
static bool containsElement(int [] arr, int n)
{
// To store the XOR of all
// the array elements
int xorArr = 0;
for (int i = 0; i < n; ++i)
xorArr ^= arr[i];
// For every element of the array
for (int i = 0; i < n; ++i)
{
// Take the XOR after excluding
// the current element
int x = xorArr ^ arr[i];
// If the XOR of the remaining elements
// is equal to the current element
if (arr[i] == x)
return true;
}
// If no such element is found
return false;
}
// Driver code
public static void Main ()
{
int [] arr = { 8, 2, 4, 15, 1 };
int n = arr.Length;
if (containsElement(arr, n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by ihritik
JavaScript
<script>
// JavaScript implementation of the approach
// Function that returns true if the array
// contains an element which is equal to
// the XOR of the remaining elements
function containsElement(arr, n)
{
// To store the XOR of all
// the array elements
let xorArr = 0;
for (let i = 0; i < n; ++i)
xorArr ^= arr[i];
// For every element of the array
for (let i = 0; i < n; ++i) {
// Take the XOR after excluding
// the current element
let x = xorArr ^ arr[i];
// If the XOR of the remaining elements
// is equal to the current element
if (arr[i] == x)
return true;
}
// If no such element is found
return false;
}
// Driver code
let arr = [ 8, 2, 4, 15, 1 ];
let n = arr.length;
if (containsElement(arr, n))
document.write("Yes");
else
document.write("No");
// This code is contributed by Surbhi Tyagi.
</script>
Time complexity: O(n) where n is size of input array
Auxiliary Space: O(1)
Similar Reads
Check if the array has an element which is equal to sum of all the remaining elements Given an array of N elements, the task is to check if the array has an element that is equal to the sum of all the remaining elements. Examples: Input: a[] = {5, 1, 2, 2} Output: Yes we can write 5=(1+2+2) Input: a[] = {2, 1, 2, 4, 3} Output: No Approach: Suppose that the total elements in the array
10 min read
Make Array elements equal by replacing adjacent elements with their XOR Given an array A[] consisting of N integers, the task is to check if it is possible to reduce array of at least length 2 such that all the elements in the array are equal. In an operation, choose any index i, and replace A[i] and A[i+1] with their XOR value. Example: Input: A[] = {0, 2, 2}Output: YE
14 min read
Check if Sum and XOR of all elements of array is equal Given an array arr[], the task is to check if sum of all elements of an array is equal to XOR of all elements of array. Example: Input: arr[] = [1, 2] Output: YES Explanation: Sum = (1+2) = 3 XOR = (1^2) = 3 Input: arr[] = [6, 3, 7, 10] Output: NO Explanation: Sum = (6 + 3 + 7 + 10) = 26 XOR = (6 ^
4 min read
XOR of all elements of array with set bits equal to K Given an array of integers and a number K. The task is to find the XOR of only those elements of the array whose total set bits are equal to K. Examples: Input : arr[] = {1, 22, 3, 10}, K=1 Output : 1 Elements with set bits equal to 1 is 1. So, XOR is also 1. Input : arr[] = {3, 4, 10, 5, 8}, K=2 Ou
4 min read
Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element Given an array arr[] of length N, the task is to find the count of array indices such that removing an element from these indices makes the Bitwise xor of odd-indexed elements and even-indexed (1-based indexing) elements are equal. Examples: Input: arr[] = {1, 0, 1, 0, 1}, N = 5Output: 3Explanation:
10 min read