📌  相关文章
📜  查找一个K长度子数组,其按位XOR等于其余数组元素的XOR

📅  最后修改于: 2021-05-25 01:18:09             🧑  作者: Mango

给定一个大小为N的数组arr [] ,任务是检查数组中是否存在任何大小为K的子数组,其按位XOR等于其余数组元素的按位XOR。如果发现是真的,则打印“是” 。否则,打印“否”

例子

天真的方法:解决此问题的最简单方法是生成大小为K的所有子数组。对于每个子数组,检查子数组的按位XOR是否等于其余元素的按位XOR。如果发现是真的,则打印“是” 。否则,打印“否”

时间复杂度: O(N 2 )
辅助空间: O(1)

高效的方法:可以使用滑动窗口技术来优化上述方法,以下是观察结果:

  • 计算所有数组元素的按位XOR,例如totalXOR
  • 计算数组的前K个元素的按位XOR ,例如SubarrayXOR
  • 使用滑动窗技术,遍历大小为K的每个子阵列,并检查子阵列的位异或等于剩余的数组元素或不属于位异或。如果发现是真的,则打印“是”
  • 否则,打印“否”

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Utility function to check if subarray
// of size K exits whose XOR of elements
// equal to XOR ofremaning array elements
bool isSubarrayExistUtil(int arr[], int K, int N)
{
 
    int totalXOR = 0;
    int SubarrayXOR = 0;
 
    // Find XOR of whole array
    for (int i = 0; i < N; i++)
        totalXOR ^= arr[i];
 
    // Find XOR of first K elements
    for (int i = 0; i < K; i++)
        SubarrayXOR ^= arr[i];
    if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
        return true;
 
    for (int i = K; i < N; i++) {
 
        // Adding XOR of next element
        SubarrayXOR ^= arr[i];
 
        // Removing XOR of previous element
        SubarrayXOR ^= arr[i - 1];
 
        // Check if XOR of current subarray matches
        // with the XOR of remaining elements or not
        if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
            return true;
    }
    return false;
}
 
// Function to check if subarray of size
// K exits whose XOR of elements equal
// to XOR ofremaning array elements
void isSubarrayExist(int arr[], int K, int N)
{
    if (isSubarrayExistUtil(arr, K, N))
        cout << "YES\n";
    else
        cout << "NO\n";
}
 
// Driver Code
int32_t main()
{
    // Given array
    int arr[] = { 2, 3, 3, 5, 7, 7, 3, 4 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given K
    int K = 5;
 
    // Function Call
    isSubarrayExist(arr, K, N);
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Utility function to check if subarray
// of size K exits whose XOR of elements
// equal to XOR ofremaning array elements
static boolean isSubarrayExistUtil(int arr[],
                                   int K, int N)
{
    int totalXOR = 0;
    int SubarrayXOR = 0;
 
    // Find XOR of whole array
    for(int i = 0; i < N; i++)
        totalXOR ^= arr[i];
 
    // Find XOR of first K elements
    for(int i = 0; i < K; i++)
        SubarrayXOR ^= arr[i];
    if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
        return true;
 
    for(int i = K; i < N; i++)
    {
         
        // Adding XOR of next element
        SubarrayXOR ^= arr[i];
 
        // Removing XOR of previous element
        SubarrayXOR ^= arr[i - 1];
 
        // Check if XOR of current subarray matches
        // with the XOR of remaining elements or not
        if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
            return true;
    }
    return false;
}
 
// Function to check if subarray of size
// K exits whose XOR of elements equal
// to XOR ofremaning array elements
static void isSubarrayExist(int arr[],
                            int K, int N)
{
    if (isSubarrayExistUtil(arr, K, N))
        System.out.print("YES\n");
    else
        System.out.print("NO\n");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array
    int arr[] = { 2, 3, 3, 5, 7, 7, 3, 4 };
 
    // Size of the array
    int N = arr.length;
 
    // Given K
    int K = 5;
 
    // Function Call
    isSubarrayExist(arr, K, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
 
# Utility function to check if subarray
# of size K exits whose XOR of elements
# equal to XOR ofremaning array elements
def isSubarrayExistUtil(arr, K, N):
    totalXOR = 0
    SubarrayXOR = 0
 
    # Find XOR of whole array
    for i in range(N):
        totalXOR ^= arr[i]
 
    # Find XOR of first K elements
    for i in range(K):
        SubarrayXOR ^= arr[i]
    if (SubarrayXOR == (totalXOR ^ SubarrayXOR)):
        return True
 
    for i in range(K, N):
 
        # Adding XOR of next element
        SubarrayXOR ^= arr[i]
 
        # Removing XOR of previous element
        SubarrayXOR ^= arr[i - 1]
 
        # Check if XOR of current subarray matches
        # with the XOR of remaining elements or not
        if (SubarrayXOR == (totalXOR ^ SubarrayXOR)):
            return True
    return False
 
# Function to check if subarray of size
# K exits whose XOR of elements equal
# to XOR ofremaning array elements
def isSubarrayExist(arr, K, N):
    if (isSubarrayExistUtil(arr, K, N)):
        print("YES")
    else:
        print("NO")
 
# Driver Code
if __name__ == '__main__':
     
    # Given array
    arr = [2, 3, 3, 5, 7, 7, 3, 4]
 
    # Size of the array
    N = len(arr)
 
    # Given K
    K = 5
 
    # Function Call
    isSubarrayExist(arr, K, N)
 
    # This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
// Utility function to check if subarray
// of size K exits whose XOR of elements
// equal to XOR ofremaning array elements
static bool isSubarrayExistUtil(int []arr,
                                   int K, int N)
{
    int totalXOR = 0;
    int SubarrayXOR = 0;
 
    // Find XOR of whole array
    for(int i = 0; i < N; i++)
        totalXOR ^= arr[i];
 
    // Find XOR of first K elements
    for(int i = 0; i < K; i++)
        SubarrayXOR ^= arr[i];
    if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
        return true;
    for(int i = K; i < N; i++)
    {
         
        // Adding XOR of next element
        SubarrayXOR ^= arr[i];
 
        // Removing XOR of previous element
        SubarrayXOR ^= arr[i - 1];
 
        // Check if XOR of current subarray matches
        // with the XOR of remaining elements or not
        if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
            return true;
    }
    return false;
}
 
// Function to check if subarray of size
// K exits whose XOR of elements equal
// to XOR ofremaning array elements
static void isSubarrayExist(int []arr,
                            int K, int N)
{
    if (isSubarrayExistUtil(arr, K, N))
        Console.Write("YES\n");
    else
        Console.Write("NO\n");
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array
    int []arr = { 2, 3, 3, 5, 7, 7, 3, 4 };
 
    // Size of the array
    int N = arr.Length;
 
    // Given K
    int K = 5;
 
    // Function Call
    isSubarrayExist(arr, K, N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
YES

时间复杂度: O(N)
辅助空间: O(N)