Minimum XOR of at most K elements in range [L, R]
Given three integers L, R, and K, the task is to find the minimum Bitwise XOR of at most K integers between [L, R].
Examples:
Input: L = 1, R = 10, K = 3
Output: 0
Explanation:
Choose elements 4, 5, and 1 in the range [1, 10] and the Bitwise XOR of the chosen elements is 0, which is minimum.
Input: L = 32, R = 33, K = 2
Output: 1
Explanation:
Choose elements 32, and 33 in the range [32, 33] and the Bitwise XOR of the chosen elements is 1, which is minimum.
Brute Force Approach:
- Initialize min_xor to INT_MAX, which represents the largest possible integer value in C++.
- Iterate over all possible values of k from 1 to K.
- For each value of k, initialize a vector to store the current combination of k integers, and initialize the first k elements of the combination to L, L+1, …, L+k-1.
- Generate all possible combinations of k integers between L and R using a modified version of the algorithm to generate combinations, where we increment the rightmost element of the combination if possible and adjust the remaining elements accordingly.
- For each combination, calculate the XOR value of the current combination by iterating over all elements of the combination and performing the XOR operation. Store the minimum XOR value encountered so far in min_xor.
- Return the final value of min_xor as the minimum bitwise XOR of at most K integers between [L, R].
C++
#include <bits/stdc++.h>
using namespace std;
int min_bitwise_xor( int L, int R, int K)
{
int min_xor = INT_MAX;
for ( int k = 1; k <= K; k++) {
vector< int > comb(k);
for ( int i = 0; i < k; i++) {
comb[i] = L + i;
}
while (comb[k - 1] <= R) {
int xor_val = 0;
for ( int num : comb) {
xor_val ^= num;
}
min_xor = min(min_xor, xor_val);
if (comb[k - 1] == R) {
break ;
}
int j = k - 1;
while (j >= 0 && comb[j] == R - k + j + 1) {
j--;
}
if (j < 0) {
break ;
}
comb[j]++;
for ( int i = j + 1; i < k; i++) {
comb[i] = comb[i - 1] + 1;
}
}
}
return min_xor;
}
int main()
{
int L = 1, R = 10, K = 3;
int min_xor = min_bitwise_xor(L, R, K);
cout << min_xor << endl;
return 0;
}
|
Python3
import sys
def min_bitwise_xor(L, R, K):
min_xor = sys.maxsize
for k in range ( 1 , K + 1 ):
comb = [i for i in range (L, L + k)]
while comb[k - 1 ] < = R:
xor_val = 0
for num in comb:
xor_val ^ = num
min_xor = min (min_xor, xor_val)
if comb[k - 1 ] = = R:
break
j = k - 1
while j > = 0 and comb[j] = = R - k + j + 1 :
j - = 1
if j < 0 :
break
comb[j] + = 1
for i in range (j + 1 , k):
comb[i] = comb[i - 1 ] + 1
return min_xor
L, R, K = 1 , 10 , 3
min_xor = min_bitwise_xor(L, R, K)
print (min_xor)
|
Java
import java.util.*;
public class Main {
public static int minBitwiseXor( int L, int R, int K) {
int minXor = Integer.MAX_VALUE;
for ( int k = 1 ; k <= K; k++) {
List<Integer> comb = new ArrayList<Integer>();
for ( int i = 0 ; i < k; i++) {
comb.add(L + i);
}
while (comb.get(k - 1 ) <= R) {
int xorVal = 0 ;
for ( int num : comb) {
xorVal ^= num;
}
minXor = Math.min(minXor, xorVal);
if (comb.get(k - 1 ) == R) {
break ;
}
int j = k - 1 ;
while (j >= 0 && comb.get(j) == R - k + j + 1 ) {
j--;
}
if (j < 0 ) {
break ;
}
comb.set(j, comb.get(j) + 1 );
for ( int i = j + 1 ; i < k; i++) {
comb.set(i, comb.get(i - 1 ) + 1 );
}
}
}
return minXor;
}
public static void main(String[] args) {
int L = 1 , R = 10 , K = 3 ;
int minXor = minBitwiseXor(L, R, K);
System.out.println(minXor);
}
}
|
C#
using System;
class Program {
static int MinBitwiseXOR( int L, int R, int K)
{
int min_xor = int .MaxValue;
for ( int k = 1; k <= K; k++) {
int [] comb = new int [k];
for ( int i = 0; i < k; i++) {
comb[i] = L + i;
}
while (comb[k - 1] <= R) {
int xor_val = 0;
foreach ( int num in comb) { xor_val ^= num; }
min_xor = Math.Min(min_xor, xor_val);
if (comb[k - 1] == R) {
break ;
}
int j = k - 1;
while (j >= 0 && comb[j] == R - k + j + 1) {
j--;
}
if (j < 0) {
break ;
}
comb[j]++;
for ( int i = j + 1; i < k; i++) {
comb[i] = comb[i - 1] + 1;
}
}
}
return min_xor;
}
static void Main( string [] args)
{
int L = 1, R = 10, K = 3;
int min_xor = MinBitwiseXOR(L, R, K);
Console.WriteLine(min_xor);
}
}
|
Javascript
function minBitwiseXor(L, R, K)
{
let minXor = Number.MAX_SAFE_INTEGER;
for (let k = 1; k <= K; k++)
{
let comb = [];
for (let i = 0; i < k; i++) {
comb.push(L + i);
}
while (comb[k - 1] <= R)
{
let xorVal = 0;
for (let num of comb) {
xorVal ^= num;
}
minXor = Math.min(minXor, xorVal);
if (comb[k - 1] == R) {
break ;
}
let j = k - 1;
while (j >= 0 && comb[j] == R - k + j + 1) {
j--;
}
if (j < 0) {
break ;
}
comb[j]++;
for (let i = j + 1; i < k; i++) {
comb[i] = comb[i - 1] + 1;
}
}
}
return minXor;
}
let L = 1;
let R = 10;
let K = 3;
let minXor = minBitwiseXor(L, R, K);
console.log(minXor);
|
Time Complexity: O((R-L+1)^K * K^2), where R-L+1 is the number of integers between L and R, and K is the maximum number of integers that can be selected.
Space Complexity: O(K)
Efficient Approach: An observation that helps us in solving the problem is the Bitwise XOR of two numbers X and (X+1) is 1 if X is an even number. Thus, the Bitwise XOR of four consecutive numbers would be 0 if the first one is even.
Follow the steps below to solve the problem:
- If the value of K is greater than 4 then the answer is always 0. (This is because four numbers X, (X+1), (X+2), and (X+3) can always be found in a range of 5 where X is an even number.)
- If the value of K is 2, call the function func2() that takes L, R, and K as input parameters and does the following:
- If the value of (R-L) is greater than or equal to 2 i.e. there are at least 3 numbers in the range, return 1.(This is because two numbers X and X+1 can always be found in a range of 3 where X is an even number.)
- Otherwise, return the minimum of L and (L^R).
- If the value of K is 3, call the function func3() that takes L, R, and K as input parameters and does the following:
- If (R^L) lies between L and R, return 0. (This is because (R^L)^L^R=0) )
- Otherwise, return func2(L, R, K)
- If the value of K is 4, call the function func4() that takes L, R, and K as input parameters and does the following:
- If (R-L) is greater than or equal to 4, i.e. there are at least 5 numbers in the range, return 0. (This is because four numbers X,(X+1),(X+2), and (X+3) can always be found in a range of 5 where X is an even number.)
- Otherwise, return the minimum of Xor of the four numbers in the range [L, R] and func3(L, R, K).
- Otherwise, return L.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int func2( int L, int R, int K)
{
if (R - L >= 2)
return 1;
return min(L, L ^ R);
}
int func3( int L, int R, int K)
{
if ((R ^ L) > L && (R ^ L) < R)
return 0;
return func2(L, R, K);
}
int func4( int L, int R, int K)
{
if (R - L >= 4)
return 0;
int minval = L ^ (L + 1) ^ (L + 2) ^ (L + 3);
return min(minval, func3(L, R, K));
}
int minimumXor( int L, int R, int K)
{
if (K > 4)
return 0;
else if (K == 4)
return func4(L, R, K);
else if (K == 3)
return func3(L, R, K);
else if (K == 2)
return func2(L, R, K);
else
return L;
}
int main()
{
int L = 1, R = 3, K = 3;
cout << minimumXor(L, R, K) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int func2( int L, int R, int K)
{
if (R - L >= 2 )
return 1 ;
return Math.min(L, L ^ R);
}
static int func3( int L, int R, int K)
{
if ((R ^ L) > L && (R ^ L) < R)
return 0 ;
return func2(L, R, K);
}
static int func4( int L, int R, int K)
{
if (R - L >= 4 )
return 0 ;
int minval = L ^ (L + 1 ) ^ (L + 2 ) ^ (L + 3 );
return Math.min(minval, func3(L, R, K));
}
static int minimumXor( int L, int R, int K)
{
if (K > 4 )
return 0 ;
else if (K == 4 )
return func4(L, R, K);
else if (K == 3 )
return func3(L, R, K);
else if (K == 2 )
return func2(L, R, K);
else
return L;
}
public static void main(String[] args)
{
int L = 1 , R = 3 , K = 3 ;
System.out.println( minimumXor(L, R, K));
}
}
|
Python3
def func2(L, R, K):
if (R - L > = 2 ):
return 1
return min (L, L ^ R)
def func3(L, R, K):
if ((R ^ L) > L and (R ^ L) < R):
return 0
return func2(L, R, K)
def func4(L, R, K):
if (R - L > = 4 ):
return 0
minval = L ^ (L + 1 ) ^ (L + 2 ) ^ (L + 3 )
return min (minval, func3(L, R, K))
def minimumXor(L, R, K):
if (K > 4 ):
return 0
elif (K = = 4 ):
return func4(L, R, K)
elif (K = = 3 ):
return func3(L, R, K)
elif (K = = 2 ):
return func2(L, R, K)
else :
return L
if __name__ = = '__main__' :
L, R, K = 1 , 3 , 3
print (minimumXor(L, R, K))
|
C#
using System;
class GFG
{
static int func2( int L, int R, int K)
{
if (R - L >= 2)
return 1;
return Math.Min(L, L ^ R);
}
static int func3( int L, int R, int K)
{
if ((R ^ L) > L && (R ^ L) < R)
return 0;
return func2(L, R, K);
}
static int func4( int L, int R, int K)
{
if (R - L >= 4)
return 0;
int minval = L ^ (L + 1) ^ (L + 2) ^ (L + 3);
return Math.Min(minval, func3(L, R, K));
}
static int minimumXor( int L, int R, int K)
{
if (K > 4)
return 0;
else if (K == 4)
return func4(L, R, K);
else if (K == 3)
return func3(L, R, K);
else if (K == 2)
return func2(L, R, K);
else
return L;
}
static void Main()
{
int L = 1, R = 3, K = 3;
Console.Write( minimumXor(L, R, K));
}
}
|
Javascript
<script>
function func2(L, R, K)
{
if (R - L >= 2)
return 1;
return Mat.min(L, L ^ R);
}
function func3(L, R, K)
{
if ((R ^ L) > L && (R ^ L) < R)
return 0;
return func2(L, R, K);
}
function func4(L, R, K)
{
if (R - L >= 4)
return 0;
var minval = L ^ (L + 1) ^
(L + 2) ^ (L + 3);
return Math.min(minval, func3(L, R, K));
}
function minimumXor(L, R, K)
{
if (K > 4)
return 0;
else if (K == 4)
return func4(L, R, K);
else if (K == 3)
return func3(L, R, K);
else if (K == 2)
return func2(L, R, K);
else
return L;
}
var L = 1, R = 3, K = 3;
document.write(minimumXor(L, R, K));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...