📜  找到一个 K 长度的子数组,其按位异或等于剩余数组元素的位异或

📅  最后修改于: 2021-09-07 02:23:53             🧑  作者: Mango

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

例子

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

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

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

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

下面是上述方法的实现:

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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live