# XOR of all Prime numbers in an Array

Given an array of integers arr[]. The task is to find the bitwise XOR of all the prime numbers present in the array.

Examples:

```Input: arr[] = {2, 5, 8, 4, 3}
Output: 4

Input: arr[] = {7, 12, 2, 6, 11}
Output: 14
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Create a sieve to check whether an element is prime or not in O(1).
• Traverse the array and check if the number is prime.
• Compute the xor of all the prime elements of the array.

Below is the implementation of the above approach:

 `// C++ program to find Xor of all ` `// Prime numbers in array ` ` `  `#include ` `using` `namespace` `std; ` ` `  `bool` `prime; ` ` `  `void` `SieveOfEratosthenes(``int` `n) ` `{ ` ` `  `    ``memset``(prime, ``true``, ``sizeof``(prime)); ` ` `  `    ``// false here indicates ` `    ``// that it is not prime ` `    ``prime = ``false``; ` ` `  `    ``for` `(``int` `p = 2; p * p <= n; p++) { ` ` `  `        ``// If prime[p] is not changed, ` `        ``// then it is a prime ` `        ``if` `(prime[p]) { ` ` `  `            ``// Update all multiples of p, ` `            ``// set them to non-prime ` `            ``for` `(``int` `i = p * 2; i <= n; i += p) ` `                ``prime[i] = ``false``; ` `        ``} ` `    ``} ` `} ` ` `  `// Function to compute xor of all ` `// prime elements ` `int` `xorPrimes(``int` `arr[], ``int` `n) ` `{ ` `    ``SieveOfEratosthenes(100005); ` ` `  `    ``int` `xorVal = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// if the element is prime ` `        ``if` `(prime[arr[i]]) ` `            ``xorVal = xorVal ^ arr[i]; ` `    ``} ` ` `  `    ``return` `xorVal; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``int` `arr[] = { 4, 3, 2, 6, 100, 17 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << xorPrimes(arr, n); ` ` `  `    ``return` `0; ` `} `

 `// Java program to find Xor of all ` `// Prime numbers in array  ` `import` `java.util.Arrays; ` ` `  ` `  `class` `GFG  ` `{ ` `    ``static` `boolean` `prime[] = ``new` `boolean``[``100005``]; ` ` `  `    ``static` `void` `SieveOfEratosthenes(``int` `n)  ` `    ``{ ` `        ``Arrays.fill(prime, ``true``); ` ` `  `        ``// false here indicates ` `        ``// that it is not prime ` `        ``prime[``1``] = ``false``; ` ` `  `        ``for` `(``int` `p = ``2``; p * p < n; p++) ` `        ``{ ` ` `  `            ``// If prime[p] is not changed, ` `            ``// then it is a prime ` `            ``if` `(prime[p]) ` `            ``{ ` `                ``// Update all multiples of p, ` `                ``// set them to non-prime ` `                ``for` `(``int` `i = p * ``2``; i < n; i += p) ` `                ``{ ` `                    ``prime[i] = ``false``; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Function to compute xor of all ` `    ``// prime elements ` `    ``static` `int` `xorPrimes(``int` `arr[], ``int` `n)  ` `    ``{ ` `        ``SieveOfEratosthenes(``100005``); ` `        ``int` `xorVal = ``0``; ` `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{ ` `            ``// if the element is prime ` `            ``if` `(prime[arr[i]])  ` `            ``{ ` `                ``xorVal = xorVal ^ arr[i]; ` `            ``} ` `        ``} ` `        ``return` `xorVal; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `arr[] = {``4``, ``3``, ``2``, ``6``, ``100``, ``17``}; ` `        ``int` `n = arr.length; ` `        ``System.out.println(xorPrimes(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by  ` `// Rajput-Ji `

 `# Python3 program to find Xor of  ` `# all Prime numbers in array  ` ` `  `prime ``=` `[``True``] ``*` `(``100005``) ` ` `  `def` `SieveOfEratosthenes(n):  ` `  `  `    ``# False here indicates  ` `    ``# that it is not prime  ` `    ``prime[``1``] ``=` `False` `    ``p ``=` `2` `     `  `    ``while` `p``*``p <``=` `n: ` ` `  `        ``# If prime[p] is not changed,  ` `        ``# then it is a prime  ` `        ``if` `prime[p]:   ` ` `  `            ``# Update all multiples of p,  ` `            ``# set them to non-prime  ` `            ``for` `i ``in` `range``(p ``*` `2``, n``+``1``, p):  ` `                ``prime[i] ``=` `False` `                 `  `        ``p ``+``=` `1` `          `  `# Function to compute xor  ` `# of all prime elements  ` `def` `xorPrimes(arr, n):  ` `  `  `    ``SieveOfEratosthenes(``100004``)  ` ` `  `    ``xorVal ``=` `0`  `    ``for` `i ``in` `range``(``0``, n):   ` ` `  `        ``# if the element is prime  ` `        ``if` `prime[arr[i]]:  ` `            ``xorVal ``=` `xorVal ^ arr[i]  ` `      `  `    ``return` `xorVal  ` `  `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``:  ` `  `  `    ``arr ``=` `[``4``, ``3``, ``2``, ``6``, ``100``, ``17``]   ` `    ``n ``=` `len``(arr)  ` ` `  `    ``print``(xorPrimes(arr, n)) ` ` `  `# This code is contributed by Rituraj Jain `

 `// C# program to find Xor of all ` `// Prime numbers in array  ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `    ``static` `bool` `[]prime = ``new` `bool``; ` ` `  `    ``static` `void` `SieveOfEratosthenes(``int` `n)  ` `    ``{ ` `        ``for``(``int` `i = 0; i < 100005; i++) ` `            ``prime[i] = ``true``; ` ` `  `        ``// false here indicates ` `        ``// that it is not prime ` `        ``prime = ``false``; ` ` `  `        ``for` `(``int` `p = 2; p * p < n; p++) ` `        ``{ ` ` `  `            ``// If prime[p] is not changed, ` `            ``// then it is a prime ` `            ``if` `(prime[p]) ` `            ``{ ` `                ``// Update all multiples of p, ` `                ``// set them to non-prime ` `                ``for` `(``int` `i = p * 2; i < n; i += p) ` `                ``{ ` `                    ``prime[i] = ``false``; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Function to compute xor of all ` `    ``// prime elements ` `    ``static` `int` `xorPrimes(``int` `[]arr, ``int` `n)  ` `    ``{ ` `        ``SieveOfEratosthenes(100005); ` `        ``int` `xorVal = 0; ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{ ` `            ``// if the element is prime ` `            ``if` `(prime[arr[i]])  ` `            ``{ ` `                ``xorVal = xorVal ^ arr[i]; ` `            ``} ` `        ``} ` `        ``return` `xorVal; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``int` `[]arr = {4, 3, 2, 6, 100, 17}; ` `        ``int` `n = arr.Length; ` `        ``Console.WriteLine(xorPrimes(arr, n)); ` `    ``} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

Output:
```16
```

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.

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.

Article Tags :
Practice Tags :