📌  相关文章
📜  用最小的异或找到大小为 K 的子数组

📅  最后修改于: 2021-09-03 03:10:08             🧑  作者: Mango

给定一个数组arr[]和整数K ,任务是找到给定数组中大小为K的任何子数组的最小按位异或和。
例子:

朴素的方法:一个简单的解决方案是将每个元素视为大小为 k 的子数组的开头,并从该元素开始计算子数组的异或。
时间复杂度: O(N * K)
高效方法:想法是使用大小为K的滑动窗口技术并跟踪当前K元素的 XOR 和。要计算当前窗口的 XOR,请使用前一个窗口的第一个元素执行 XOR 以丢弃该元素,并使用当前元素将此元素添加到窗口中。类似地,滑动窗口以找到大小为K的子数组的最小异或。
下面是上述方法的实现:

C++
// C++ implementation to find the
// subarray with minimum XOR
 
#include 
 
using namespace std;
     
// Function to find the minimum
// XOR of the subarray of size K
void findMinXORSubarray(int arr[],
                     int n, int k)
{
    // K must be smaller
    // than or equal to n
    if (n < k)
        return;
 
    // Initialize beginning
    // index of result
    int res_index = 0;
 
    // Compute XOR sum of first
    // subarray of size K
    int curr_xor = 0;
    for (int i = 0; i < k; i++)
        curr_xor ^= arr[i];
 
    // Initialize minimum XOR
    // sum as current xor
    int min_xor = curr_xor;
 
    // Traverse from (k+1)'th
    // element to n'th element
    for (int i = k; i < n; i++) {
         
        // XOR with current item
        // and first item of
        // previous subarray
        curr_xor ^= (arr[i] ^ arr[i - k]);
 
        // Update result if needed
        if (curr_xor < min_xor) {
            min_xor = curr_xor;
            res_index = (i - k + 1);
        }
    }
 
    cout << min_xor << "\n";
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 7, 90, 20, 10, 50, 40 };
    int k = 3; // Subarray size
    int n = sizeof arr / sizeof arr[0];
     
    // Function Call
    findMinXORSubarray(arr, n, k);
    return 0;
}


Java
// Java implementation to find the
// subarray with minimum XOR
class GFG{
     
// Function to find the minimum
// XOR of the subarray of size K
static void findMinXORSubarray(int arr[],
                               int n, int k)
{
     
    // K must be smaller
    // than or equal to n
    if (n < k)
        return;
 
    // Initialize beginning
    // index of result
    int res_index = 0;
 
    // Compute XOR sum of first
    // subarray of size K
    int curr_xor = 0;
    for(int i = 0; i < k; i++)
       curr_xor ^= arr[i];
 
    // Initialize minimum XOR
    // sum as current xor
    int min_xor = curr_xor;
 
    // Traverse from (k+1)'th
    // element to n'th element
    for(int i = k; i < n; i++)
    {
        
       // XOR with current item
       // and first item of
       // previous subarray
       curr_xor ^= (arr[i] ^ arr[i - k]);
        
       // Update result if needed
       if (curr_xor < min_xor)
       {
           min_xor = curr_xor;
           res_index = (i - k + 1);
       }
    }
    System.out.print(min_xor + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 3, 7, 90, 20, 10, 50, 40 };
     
    // Subarray size
    int k = 3;
    int n = arr.length;
     
    // Function Call
    findMinXORSubarray(arr, n, k);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 implementation to find the
# subarray with minimum XOR
     
# Function to find the minimum
# XOR of the subarray of size K
def findMinXORSubarray(arr, n, k):
 
    # K must be smaller
    # than or equal to n
    if (n < k):
        return
 
    # Initialize beginning
    # index of result
    res_index = 0
 
    # Compute XOR sum of first
    # subarray of size K
    curr_xor = 0
    for i in range(0, k):
        curr_xor = curr_xor ^ arr[i]
 
    # Initialize minimum XOR
    # sum as current xor
    min_xor = curr_xor
 
    # Traverse from (k+1)'th
    # element to n'th element
    for i in range(k, n):
         
        # XOR with current item
        # and first item of
        # previous subarray
        curr_xor ^= (arr[i] ^ arr[i - k])
 
        # Update result if needed
        if (curr_xor < min_xor):
            min_xor = curr_xor
            res_index = (i - k + 1)
 
    print(min_xor, end = '\n')
 
# Driver Code
arr = [ 3, 7, 90, 20, 10, 50, 40 ]
 
# Subarray size
k = 3
n = len(arr)
 
# Function Call
findMinXORSubarray(arr, n, k)
 
# This code is contributed by PratikBasu


C#
// C# implementation to find the
// subarray with minimum XOR
using System;
 
class GFG{
     
// Function to find the minimum
// XOR of the subarray of size K
static void findMinXORSubarray(int []arr,
                               int n, int k)
{
     
    // K must be smaller
    // than or equal to n
    if (n < k)
        return;
 
    // Initialize beginning
    // index of result
    int res_index = 0;
 
    // Compute XOR sum of first
    // subarray of size K
    int curr_xor = 0;
    for(int i = 0; i < k; i++)
       curr_xor ^= arr[i];
 
    // Initialize minimum XOR
    // sum as current xor
    int min_xor = curr_xor;
 
    // Traverse from (k+1)'th
    // element to n'th element
    for(int i = k; i < n; i++)
    {
        
       // XOR with current item
       // and first item of
       // previous subarray
       curr_xor ^= (arr[i] ^ arr[i - k]);
        
       // Update result if needed
       if (curr_xor < min_xor)
       {
           min_xor = curr_xor;
           res_index = (i - k + 1);
       }
    }
    Console.Write(min_xor + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 3, 7, 90, 20, 10, 50, 40 };
     
    // Subarray size
    int k = 3;
    int n = arr.Length;
     
    // Function Call
    findMinXORSubarray(arr, n, k);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:

16

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

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