Maximum XOR subarray of K distinct integer
Given an array A consisting of N positive integers, the task is to calculate the maximum XOR of the subarray of size K consisting of all distinct integers. If such subarray is not present then Print “-1”.
Examples:
Input: N = 10, A[] = [2, 3, 4, 2, 4, 5, 7, 4, 3, 9], K = 4
Output: 9
Explanation : All Subarrays of size K with all distinct integers are [2, 4, 5, 7], [5, 7, 4, 3], [7, 4, 3, 9] and there XOR are 6, 5, 9 respectively. So Maximum XOR of subarray is 9.Input: N = 5, A[] = [2, 3, 2, 3, 2], K = 3
Output: -1
Explanation: Since there of no subarray of size K with all integers distinct
Naive Solution: The basic way to solve the problem is as follows:
A Simple Solution is to generate all subarrays of size K, check if all the integers in that subarray are distinct, compute their XORs, and finally return the maximum of all XORs, if no subarray of size K with all distinct is found return -1.
Below is the implementation of the above approach:
C++
|
Maximum XOR of subarray of size K with all distincts are : 9
Time Complexity: O(N*K)
Auxiliary Space: O(K)
Efficient Approach: To solve the problem follow the below idea:
In this approach, we will use the XOR property that ( X XOR X ) is 0 and we have to take a map that will store the frequencies of the integers and use the 2-pointer approach, whenever we get the element whose frequency exceeds 1 or whenever the size the map will exceed K then we will shrink window otherwise we will expand the window.
Below is the implementation of the above approach:
C++
|
Maximum XOR of subarray of size K with all distict are : 9
Time Complexity: O(N*Log(K))
Auxiliary Space: O(K)
Given an array A consisting of N positive integers, the task is to calculate the maximum XOR of the subarray of size K consisting of all distinct integers. If such subarray is not present then Print “-1”.
Examples:
Input: N = 10, A[] = [2, 3, 4, 2, 4, 5, 7, 4, 3, 9], K = 4
Output: 9
Explanation : All Subarrays of size K with all distinct integers are [2, 4, 5, 7], [5, 7, 4, 3], [7, 4, 3, 9] and there XOR are 6, 5, 9 respectively. So Maximum XOR of subarray is 9.Input: N = 5, A[] = [2, 3, 2, 3, 2], K = 3
Output: -1
Explanation: Since there of no subarray of size K with all integers distinct
Naive Solution: The basic way to solve the problem is as follows:
A Simple Solution is to generate all subarrays of size K, check if all the integers in that subarray are distinct, compute their XORs, and finally return the maximum of all XORs, if no subarray of size K with all distinct is found return -1.
Below is the implementation of the above approach:
C++
|
Maximum XOR of subarray of size K with all distincts are : 9
Time Complexity: O(N*K)
Auxiliary Space: O(K)
Efficient Approach: To solve the problem follow the below idea:
In this approach, we will use the XOR property that ( X XOR X ) is 0 and we have to take a map that will store the frequencies of the integers and use the 2-pointer approach, whenever we get the element whose frequency exceeds 1 or whenever the size the map will exceed K then we will shrink window otherwise we will expand the window.
Below is the implementation of the above approach:
C++
|
Maximum XOR of subarray of size K with all distict are : 9
Time Complexity: O(N*Log(K))
Auxiliary Space: O(K)