给定数组arr []和整数K ,任务是找到给定数组中大小为K的任何子数组的最小按位XOR和。
例子:
Input: arr[] = {3, 7, 90, 20, 10, 50, 40}, K = 3
Output: 16
Explanation:
The subarray {10, 50, 40} has the minimum XOR
Input: arr[] = {3, 7, 5, 20, -10, 0, 12}, K = 2
Output: 17
Explanation:
The subarray {5, 20} has the minimum XOR
天真的方法:一个简单的解决方案是将每个元素视为大小为k的子数组的开始,并计算从该元素开始的子数组的XOR。
时间复杂度: O(N * K)
高效方法:想法是使用大小为K的滑动窗口技术并跟踪当前K个元素的XOR和。要计算当前窗口的XOR,请与前一个窗口的第一个元素进行XOR以丢弃该元素,并与当前元素进行异或以将该元素添加到窗口中。同样,滑动窗口以找到大小为K的子数组的最小XOR。
下面是上述方法的实现:
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
输出:
16
时间复杂度: O(N)
辅助空间: O(1)