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

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 next Smaller of next Greater in an array
- Check if the array has an element which is equal to XOR of remaining elements
- Find XOR of two number without using XOR operator
- Count of Array elements greater than all elements on its left and next K elements on its right
- Replace every array element by Bitwise Xor of previous and next element
- Minimum Bitwise XOR operations to make any two array elements equal
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Delete array elements which are smaller than next or become smaller
- Replace array elements by sum of next two consecutive elements
- Arrange array elements such that last digit of an element is equal to first digit of the next element
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of values of x <= n for which (n XOR x) = (n - x)
- Minimum flips in two binary arrays so that their XOR is equal to another array
- XOR of two numbers after making length of their binary representations equal
- Count minimum bits to flip such that XOR of A and B equal to C
- Count numbers whose difference with N is equal to XOR with N
- Count numbers whose XOR with N is equal to OR with N
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Count subarrays with sum equal to its XOR value
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively

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.