Open In App

Find the subarray of size K with minimum XOR

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] and integer K, the task is to find the minimum bitwise XOR sum of any subarray of size K in the given array.
Examples: 
 

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 
 

 

Naive Approach: A Simple Solution is to consider every element as the beginning of subarray of size k and compute XOR of subarray starting with this element. 
Time complexity: O(N * K)
Efficient Approach: The idea is to use the sliding window technique of size K and keep track of XOR sum of current K elements. To compute the XOR of the current window, perform XOR with the first element of the previous window to discard that element and with the current element to add this element into the window. Similarly, slide the windows to find the minimum XOR of the subarray of size K.
Below is the implementation of above approach:
 

C++




// C++ implementation to find the
// subarray with minimum XOR
  
#include <bits/stdc++.h>
  
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




<script>
  
// Javascript implementation to find the
// subarray with minimum XOR
      
// Function to find the minimum 
// XOR of the subarray of size K
function findMinXORSubarray(arr, n, k)
{
    // K must be smaller 
    // than or equal to n
    if (n < k)
        return;
  
    // Initialize beginning 
    // index of result
    let res_index = 0;
  
    // Compute XOR sum of first 
    // subarray of size K
    let curr_xor = 0;
    for (let i = 0; i < k; i++)
        curr_xor ^= arr[i];
  
    // Initialize minimum XOR 
    // sum as current xor
    let min_xor = curr_xor;
  
    // Traverse from (k+1)'th 
    // element to n'th element
    for (let 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);
        }
    }
  
    document.write(min_xor + "<br>");
}
  
// Driver Code
    let arr = [ 3, 7, 90, 20, 10, 50, 40 ];
    let k = 3; // Subarray size
    let n = arr.length;
      
    // Function Call
    findMinXORSubarray(arr, n, k);
  
</script>


Output: 

16

 

Time Complexity: O(N) 
Auxiliary Space: O(1)
 

Related Topic: Subarrays, Subsequences, and Subsets in Array



Last Updated : 11 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads