📌  相关文章
📜  通过X个给定运算对至少K个元素超过所有给定数组元素的XOR的数组进行计数

📅  最后修改于: 2021-05-17 18:36:59             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是计算至少X个元素比所有数组元素的XOR大的数组的数目,该数组是通过执行以下X次操作生成的。

  • 从给定数组中选择第一个或最后一个元素。
  • 将所选元素增加1或删除所选元素。

例子:

方法:想法是使用回溯方法来递归地尝试所有可能的移动,并在获得所需数组时增加计数。可能的动作是:

  • 初始化一个变量,例如xorValue ,以计算原始数组的XOR。
  • 初始化一个变量,例如count ,以存储所需数组的最终计数。
  • 递归尝试以下所有四种可能性,并在获得所需数组时增加计数:
    • 删除数组的第一个元素。
    • 删除数组的最后一个元素。
    • 将第一个数组元素增加一个。
    • 将最后一个数组元素加1。
  • 打印最终计数作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Stores the final answer
int ans = 0;
 
// Utility function to count arrays
// having at least K elements exceeding
// XOR of all given array elements
void countArraysUtil(vector& arr,
                     int X, int K,
                     int xorVal)
{
    // If no operations are left
    if (X == 0) {
 
        // Stores the count of
        // possible arrays
        int cnt = 0;
 
        // Count array elements are
        // greater than XOR
        for (int i = 0; i < arr.size(); i++) {
 
            if (arr[i] > xorVal)
                cnt++;
        }
        if (cnt >= K)
            ans++;
        return;
    }
     
    // Stores first element
    int temp = arr[0];
 
    // Delete first element
    arr.erase(arr.begin());
 
    // Recursive call
    countArraysUtil(arr, X - 1,
                    K, xorVal);
 
    // Insert first element into vector
    arr.insert(arr.begin(), temp);
 
    // Stores the last element
    temp = arr.back();
 
    // Remove last element from vector
    arr.pop_back();
 
    // Recursive call
    countArraysUtil(arr, X - 1,
                    K, xorVal);
 
    // Push last element into vector
    arr.push_back(temp);
 
    // Increment first element
    arr[0]++;
   
    // Recursive call
    countArraysUtil(arr, X - 1,
                    K, xorVal);
   
    // Decrement first element
    arr[0]--;
 
    // Increment last element
    arr[arr.size() - 1]++;
   
    // Recursive call
    countArraysUtil(arr, X - 1,
                    K, xorVal);
   
    // Decrement last element
    arr[arr.size() - 1]--;
}
 
// Function to find the count of
// arrays having atleast K elements
// greater than XOR of array
void countArrays(vector& arr,
                 int X, int K)
{
    // Stores the XOR value
// of original array
    int xorVal = 0;
 
    // Traverse the vector
    for (int i = 0; i < arr.size(); i++)
        xorVal = xorVal ^ arr[i];
 
    countArraysUtil(arr, X, K, xorVal);
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given vector
    vector arr = { 10, 2, 10, 5 };
 
    // Given value of X & K
    int X = 3, K = 3;
 
    countArrays(arr, X, K);
 
    return 0;
}


Python3
# Python program for the above approach
 
# Stores the final answer
ans = 0
 
# Utility function to count arrays
# having at least K elements exceeding
# XOR of all given array elements
def countArraysUtil( arr, X, K, xorVal):
    global ans
     
    # If no operations are left
    if (X == 0):
       
        # Stores the count of
        # possible arrays
        cnt = 0
 
        # Count array elements are
        # greater than XOR
        for i in range(len(arr)):
            if (arr[i] > xorVal):
                cnt += 1
        if (cnt >= K):
            ans += 1
        return
     
    # Stores first element
    temp = arr[0]
 
    # Delete first element
    arr.pop(0)
 
    # Recursive call
    countArraysUtil(arr, X - 1, K, xorVal)
 
    # Insert first element into vector
    arr.insert(0, temp)
 
    # Stores the last element
    temp = arr[-1]
 
    # Remove last element from vector
    arr.pop()
 
    # Recursive call
    countArraysUtil(arr, X - 1, K, xorVal)
 
    # Push last element into vector
    arr.append(temp)
 
    # Increment first element
    arr[0] += 1
   
    # Recursive call
    countArraysUtil(arr, X - 1,K, xorVal)
   
    # Decrement first element
    arr[0] -= 1
 
    # Increment last element
    arr[len(arr) - 1] += 1
   
    # Recursive call
    countArraysUtil(arr, X - 1, K, xorVal)
   
    # Decrement last element
    arr[len(arr) - 1] -= 1
 
# Function to find the count of
# arrays having atleast K elements
# greater than XOR of array
def countArrays(arr, X, K):
   
    # Stores the XOR value
    # of original array
    xorVal = 0
 
    # Traverse the vector
    for i in range(len(arr)):
        xorVal = xorVal ^ arr[i]
    countArraysUtil(arr, X, K, xorVal)
 
    # Print the answer
    print(ans)
     
# Driver Code
# Given vector
arr = [ 10, 2, 10, 5 ]
 
# Given value of X & K
X = 3
K = 3
countArrays(arr, X, K)
 
# This code is contributed by rohitsingh07052.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
   
    // Stores the final answer
    static int ans = 0;
 
    // Utility function to count arrays
    // having at least K elements exceeding
    // XOR of all given array elements
    static void countArraysUtil(List arr, int X, int K,
                                int xorVal)
    {
        // If no operations are left
        if (X == 0) {
 
            // Stores the count of
            // possible arrays
            int cnt = 0;
 
            // Count array elements are
            // greater than XOR
            for (int i = 0; i < arr.Count; i++) {
 
                if (arr[i] > xorVal)
                    cnt++;
            }
            if (cnt >= K)
                ans++;
            return;
        }
 
        // Stores first element
        int temp = arr[0];
 
        // Delete first element
        arr.RemoveAt(0);
 
        // Recursive call
        countArraysUtil(arr, X - 1, K, xorVal);
 
        // Insert first element into vector
        arr.Insert(0, temp);
 
        // Stores the last element
        temp = arr[arr.Count - 1];
 
        // Remove last element from vector
        arr.RemoveAt(arr.Count - 1);
 
        // Recursive call
        countArraysUtil(arr, X - 1, K, xorVal);
 
        // Push last element into vector
        arr.Add(temp);
 
        // Increment first element
        arr[0]++;
 
        // Recursive call
        countArraysUtil(arr, X - 1, K, xorVal);
 
        // Decrement first element
        arr[0]--;
 
        // Increment last element
        arr[arr.Count - 1]++;
 
        // Recursive call
        countArraysUtil(arr, X - 1, K, xorVal);
 
        // Decrement last element
        arr[arr.Count - 1]--;
    }
 
    // Function to find the count of
    // arrays having atleast K elements
    // greater than XOR of array
    static void countArrays(List arr, int X, int K)
    {
        // Stores the XOR value
        // of original array
        int xorVal = 0;
 
        // Traverse the vector
        for (int i = 0; i < arr.Count; i++)
            xorVal = xorVal ^ arr[i];
 
        countArraysUtil(arr, X, K, xorVal);
 
        // Print the answer
        Console.Write(ans);
    }
 
    // Driver Code
    public static void Main()
    {
       
        // Given vector
        List arr = new List() { 10, 2, 10, 5 };
 
        // Given value of X & K
        int X = 3, K = 3;
        countArrays(arr, X, K);
    }
}
 
// This code is contributed by chitranayal.


输出:
1

时间复杂度: O(4 K * N)
辅助空间: O(1)