📌  相关文章
📜  等于下两个元素的XOR的元素计数

📅  最后修改于: 2021-05-25 03:13:26             🧑  作者: Mango

给定n个元素的数组arr [] 。任务是找到等于下两个元素的XOR的元素数。
例子:

方法:初始化count = 0 ,对于数组的每个元素,使其至少有两个元素出现在数组之后,如果它等于接下来的两个元素的XOR,则增加count
下面是上述方法的实现:

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 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;
}


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


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


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


Javascript


输出:
1