# Count of elements which are equal to the XOR of the next two elements

Given an array **arr[]** of **n** elements. The task is to find the count of elements which are equal to the XOR of the next two elements.

**Examples:**

Input:arr[] = {4, 2, 1, 3, 7, 8}

Output:1

2 is the only valid element as 1 ^ 3 = 2

Input:arr[] = {23, 1, 7, 8, 6}

Output:0

**Approach:** Initialise **count = 0** and for every element of the array such that it has at least two elements appearing after it in the array, if it is equal to the XOR of the next two elements then increment the **count**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of elements ` `// which are equal to the XOR ` `// of the next two elements ` `int` `cntElements(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// To store the required count ` ` ` `int` `cnt = 0; ` ` ` ` ` `// For every element of the array such that ` ` ` `// it has at least two elements appearing ` ` ` `// after it in the array ` ` ` `for` `(` `int` `i = 0; i < n - 2; i++) { ` ` ` ` ` `// If current element is equal to the XOR ` ` ` `// of the next two elements in the array ` ` ` `if` `(arr[i] == (arr[i + 1] ^ arr[i + 2])) { ` ` ` `cnt++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 4, 2, 1, 3, 7, 8 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `cout << cntElements(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of elements ` `// which are equal to the XOR ` `// of the next two elements ` `static` `int` `cntElements(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// To store the required count ` ` ` `int` `cnt = ` `0` `; ` ` ` ` ` `// For every element of the array such that ` ` ` `// it has at least two elements appearing ` ` ` `// after it in the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n - ` `2` `; i++) ` ` ` `{ ` ` ` ` ` `// If current element is equal to the XOR ` ` ` `// of the next two elements in the array ` ` ` `if` `(arr[i] == (arr[i + ` `1` `] ^ arr[i + ` `2` `])) ` ` ` `{ ` ` ` `cnt++; ` ` ` `} ` ` ` `} ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `arr[] = { ` `4` `, ` `2` `, ` `1` `, ` `3` `, ` `7` `, ` `8` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println (cntElements(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by jit_t ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of elements ` `# which are equal to the XOR ` `# of the next two elements ` `def` `cntElements(arr, n): ` ` ` ` ` `# To store the required count ` ` ` `cnt ` `=` `0` ` ` ` ` `# For every element of the array such that ` ` ` `# it has at least two elements appearing ` ` ` `# after it in the array ` ` ` `for` `i ` `in` `range` `(n ` `-` `2` `): ` ` ` ` ` `# If current element is equal to the XOR ` ` ` `# of the next two elements in the array ` ` ` `if` `(arr[i] ` `=` `=` `(arr[i ` `+` `1` `] ^ arr[i ` `+` `2` `])): ` ` ` `cnt ` `+` `=` `1` ` ` ` ` `return` `cnt ` ` ` `# Driver code ` `arr ` `=` `[` `4` `, ` `2` `, ` `1` `, ` `3` `, ` `7` `, ` `8` `] ` `n ` `=` `len` `(arr) ` ` ` `print` `(cntElements(arr, n)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of elements ` `// which are equal to the XOR ` `// of the next two elements ` `static` `int` `cntElements(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` ` ` `// To store the required count ` ` ` `int` `cnt = 0; ` ` ` ` ` `// For every element of the array such that ` ` ` `// it has at least two elements appearing ` ` ` `// after it in the array ` ` ` `for` `(` `int` `i = 0; i < n - 2; i++) ` ` ` `{ ` ` ` ` ` `// If current element is equal to the XOR ` ` ` `// of the next two elements in the array ` ` ` `if` `(arr[i] == (arr[i + 1] ^ arr[i + 2])) ` ` ` `{ ` ` ` `cnt++; ` ` ` `} ` ` ` `} ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main (String[] args) ` `{ ` ` ` `int` `[]arr = { 4, 2, 1, 3, 7, 8 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.WriteLine(cntElements(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

1

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count of Array elements to be divided by 2 to make at least K elements equal
- Count elements such that there are exactly X elements with values greater than or equal to X
- Count sub-arrays which have elements less than or equal to X
- Count elements less than or equal to a given value in a sorted rotated array
- Count of smaller or equal elements in sorted array
- Count pairs in an array such that both elements has equal set bits
- Minimum elements to be removed to make count of 0s and 1s equal
- Count of index pairs with equal elements in an array
- Count subarrays with equal number of occurrences of two given elements
- Find the difference of count of equal elements on the right and the left for each element
- Noble integers in an array (count of greater elements is equal to value)
- Count of elements such that difference between sum of left and right sub arrays is equal to a multiple of k
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Rearrange array elements such that Bitwise AND of first N - 1 elements is equal to last element
- Count of operations to make all elements of array a[] equal to its min element by performing a[i] – b[i]
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Position of Elements which are equal to sum of all Preceding elements
- Index of the elements which are equal to the sum of all succeeding elements
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Count of array elements which is smaller than both its adjacent 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.