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

• Difficulty Level : Medium
• Last Updated : 27 Apr, 2021

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

Input: arr[] = {4, 2, 1, 3, 7, 8}
Output:
2 is the only valid element as 1 ^ 3 = 2
Input: arr[] = {23, 1, 7, 8, 6}
Output:

Approach: Initialize 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 using namespace std; // Function to return the count of elements// which are equal to the XOR// of the next two elementsint 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 codeint main(){    int arr[] = { 4, 2, 1, 3, 7, 8 };    int n = sizeof(arr) / sizeof(int);     cout << cntElements(arr, n);     return 0;}

Java

 // Java implementation of the approachimport java.io.*; class GFG{     // Function to return the count of elements// which are equal to the XOR// of the next two elementsstatic 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 codepublic 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

Python3

 # Python3 implementation of the approach # Function to return the count of elements# which are equal to the XOR# of the next two elementsdef 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 codearr = [4, 2, 1, 3, 7, 8]n = len(arr) print(cntElements(arr, n)) # This code is contributed by Mohit Kumar

C#

 // C# implementation of the approachusing System;using System.Collections.Generic;     class GFG{     // Function to return the count of elements// which are equal to the XOR// of the next two elementsstatic 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 codepublic 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

Javascript


Output:
1

My Personal Notes arrow_drop_up