Minimum XOR of at most K elements in range [ L , R ] | C ++ program for the above approach ; Function for K = 2 ; Function for K = 2 ; Function for K = 2 ; Function to calculate the minimum XOR of at most K elements in [ L , R ] ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE 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 ; }