Skip to content
Related Articles

Related Articles

Improve Article

Minimum XOR of at most K elements in range [L, R]

  • Difficulty Level : Medium
  • Last Updated : 30 Jun, 2021

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.

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function for K=2
int func2(int L, int R, int K)
{
    if (R - L >= 2)
        return 1;
    return min(L, L ^ R);
}
// Function for K=2
int func3(int L, int R, int K)
{
    if ((R ^ L) > L && (R ^ L) < R)
        return 0;
    return func2(L, R, K);
}
// Function for K=2
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));
}
// Function to calculate the minimum XOR of at most K
// elements in [L, R]
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;
}
// Driver code
int main()
{
    // Input
    int L = 1, R = 3, K = 3;
    // Function call
    cout << minimumXor(L, R, K) << endl;
    return 0;
}

Java




// Java program for the above approach
import java.io.*;
 
class GFG
{
 
// Function for K=2
static int func2(int L, int R, int K)
{
    if (R - L >= 2)
        return 1;
    return Math.min(L, L ^ R);
}
    
// Function for K=2
static int func3(int L, int R, int K)
{
    if ((R ^ L) > L && (R ^ L) < R)
        return 0;
    return func2(L, R, K);
}
    
// Function for K=2
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));
}
// Function to calculate the minimum XOR of at most K
// elements in [L, R]
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;
}
 
  // Driver code
  public static void main(String[] args)
  {
      // Input
    int L = 1, R = 3, K = 3;
    
    // Function call
    System.out.println( minimumXor(L, R, K));
  }
}
 
// This code is contributed by sanjoy_62.

Python3




# Python program for the above approach
 
# Function for K=2
def func2(L, R, K):
    if (R - L >= 2):
        return 1
    return min(L, L ^ R)
 
  # Function for K=2
def func3(L, R, K):
    if ((R ^ L) > L and (R ^ L) < R):
        return 0
    return func2(L, R, K)
   
# Function for K=2
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))
   
# Function to calculate the minimum XOR of at most K
# elements in [L, R]
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
 
# Driver code
if __name__ == '__main__':
   
    # Input
    L, R, K = 1, 3, 3
     
    # Function call
    print (minimumXor(L, R, K))
 
# This code is contributed by mohit kumar 29.

C#




// C# program for the above approach
using System;
class GFG
{
 
// Function for K=2
static int func2(int L, int R, int K)
{
    if (R - L >= 2)
        return 1;
    return Math.Min(L, L ^ R);
}
   
// Function for K=2
static int func3(int L, int R, int K)
{
    if ((R ^ L) > L && (R ^ L) < R)
        return 0;
    return func2(L, R, K);
}
   
// Function for K=2
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));
}
// Function to calculate the minimum XOR of at most K
// elements in [L, R]
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;
}
 
 
// Driver code
static void Main()
{
    // Input
    int L = 1, R = 3, K = 3;
   
    // Function call
    Console.Write( minimumXor(L, R, K));
 
}
}
 
// This code is contributed by code_hunt.

Javascript




<script>
 
// Javascript program for the above approach
 
// Function for K=2
function func2(L, R, K)
{
    if (R - L >= 2)
        return 1;
         
    return Mat.min(L, L ^ R);
}
 
// Function for K=2
function func3(L, R, K)
{
    if ((R ^ L) > L && (R ^ L) < R)
        return 0;
         
    return func2(L, R, K);
}
 
// Function for K=2
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 to calculate the minimum XOR
// of at most K elements in [L, R]
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;
}
 
// Driver code
 
// Input
var L = 1, R = 3, K = 3;
 
// Function call
document.write(minimumXor(L, R, K));
 
// This code is contributed by SURENDRA_GANGWAR
 
</script>
Output
0

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

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :