Skip to main content
added 70 characters in body
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Here is the challenge:

Given two integers: L and R,

find the maximal values of A xor B given, L ≤ A ≤ B ≤ R

Input Format The input contains two lines, L is present in the first line. R in the second line.

Constraints 1 ≤ L ≤ R ≤ 103

Output Format The maximal value as mentioned in the problem statement.

Sample Input#00

 1
 10

Sample Output#00

15

**Sample Input**#01
10
15

Sample Output#01

7


Explanation for the second example is as follows:

In the second sample let's say L=10, R=15, then all pairs which comply to above condition are

10⊕10=0
10⊕11=1
10⊕12=6
10⊕13=7
10⊕14=4
10⊕15=5
11⊕11=0
11⊕12=7
11⊕13=6
11⊕14=5
11⊕15=4
12⊕12=0
12⊕13=1
12⊕14=2
12⊕15=3
13⊕13=0
13⊕14=3
13⊕15=2
14⊕14=0
14⊕15=1
15⊕15=0

Here two pairs (10,13) and (11,12) have maximum xor value 7 and this is the answer.

The code below represents my solution to the problem in question. It works but I feel like I reinvintedreinvented the wheel on this one. Is there a better way of going about it than what I have?

Challenge Prompt

Given two integers: L and R,

find the maximal values of A xor B given, L ≤ A ≤ B ≤ R

Input Format The input contains two lines, L is present in the first line. R in the second line.

Constraints 1 ≤ L ≤ R ≤ 103

Output Format The maximal value as mentioned in the problem statement.

Sample Input#00

 1
 10

Sample Output#00

15

**Sample Input**#01
10
15

Sample Output#01

7

Explanation for the second example is as follows:

In the second sample let's say L=10, R=15, then all pairs which comply to above condition are

10⊕10=0
10⊕11=1
10⊕12=6
10⊕13=7
10⊕14=4
10⊕15=5
11⊕11=0
11⊕12=7
11⊕13=6
11⊕14=5
11⊕15=4
12⊕12=0
12⊕13=1
12⊕14=2
12⊕15=3
13⊕13=0
13⊕14=3
13⊕15=2
14⊕14=0
14⊕15=1
15⊕15=0

Here two pairs (10,13) and (11,12) have maximum xor value 7 and this is the answer.

The code below represents my solution to the problem in question. It works but I feel like I reinvinted the wheel on this one. Is there a better way of going about it than what I have?

Challenge Prompt

Given two integers: L and R,

find the maximal values of A xor B given, L ≤ A ≤ B ≤ R

Input Format The input contains two lines, L is present in the first line. R in the second line.

Constraints 1 ≤ L ≤ R ≤ 103

Output Format The maximal value as mentioned in the problem statement.

Sample Input#00

 1
 10

Sample Output#00

15

**Sample Input**#01
10
15

Sample Output#01

7

Explanation for the second example is as follows:

In the second sample let's say L=10, R=15, then all pairs which comply to above condition are

10⊕10=0
10⊕11=1
10⊕12=6
10⊕13=7
10⊕14=4
10⊕15=5
11⊕11=0
11⊕12=7
11⊕13=6
11⊕14=5
11⊕15=4
12⊕12=0
12⊕13=1
12⊕14=2
12⊕15=3
13⊕13=0
13⊕14=3
13⊕15=2
14⊕14=0
14⊕15=1
15⊕15=0

Here two pairs (10,13) and (11,12) have maximum xor value 7 and this is the answer.

Here is the challenge:

Given two integers: L and R,

find the maximal values of A xor B given, L ≤ A ≤ B ≤ R

Input Format The input contains two lines, L is present in the first line. R in the second line.

Constraints 1 ≤ L ≤ R ≤ 103

Output Format The maximal value as mentioned in the problem statement.

Sample Input#00

 1
 10

Sample Output#00

15

**Sample Input**#01
10
15

Sample Output#01

7


Explanation for the second example is as follows:

In the second sample let's say L=10, R=15, then all pairs which comply to above condition are

10⊕10=0
10⊕11=1
10⊕12=6
10⊕13=7
10⊕14=4
10⊕15=5
11⊕11=0
11⊕12=7
11⊕13=6
11⊕14=5
11⊕15=4
12⊕12=0
12⊕13=1
12⊕14=2
12⊕15=3
13⊕13=0
13⊕14=3
13⊕15=2
14⊕14=0
14⊕15=1
15⊕15=0

Here two pairs (10,13) and (11,12) have maximum xor value 7 and this is the answer.

The code below represents my solution to the problem in question. It works but I feel like I reinvented the wheel on this one. Is there a better way of going about it than what I have?

added 2 characters in body
Source Link
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;


class Solution {

    public static List<int[]> Combinations( List<int> number_list ) 
    {
    
        // We are only choosing 2 values out of any list of numbers
        int[] TwoList = new int[2]; 

        List<int[]> result = new List<int[]>();

        List<int> stack = new List<int>(number_list);                    
                
        while (stack.Count > 0) 
        {

            int StackLast = stack.Count - 1;

            for (int i =0; i < stack.Count; i++) 
            {
                TwoList[0] = stack[StackLast];

                TwoList[1] = stack[i];

                result.Add(new int[] {TwoList[0], TwoList[1]});
            }
            stack.RemoveAt(StackLast);
        }
        
        return result;

    }

    static int MaxXor(int l, int r) 
    {
            
        List<int> NumList = new List<int>(Enumerable.Range(l, r - l + 1));

        // Inserts the list combinations as int arrays
        List<int[]> ComboListcomboList = Combinations(NumList); 

        int max = 0;

        foreach(var two in NumListcomboList)
        {
        
            int XorValue = two[0] ^ two[1];

            max = ( max > XorValue )? max : XorValue;
        }
    
        return max;
    }

    static void Main(String[] args) 
    {

        int _l;
        int _r;

        _l = Convert.ToInt32(Console.ReadLine());
         
        _r = Convert.ToInt32(Console.ReadLine());
    
        Console.WriteLine(MaxXor(_l, _r));
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;


class Solution {

    public static List<int[]> Combinations( List<int> number_list ) 
    {
    
        // We are only choosing 2 values out of any list of numbers
        int[] TwoList = new int[2]; 

        List<int[]> result = new List<int[]>();

        List<int> stack = new List<int>(number_list);                    
                
        while (stack.Count > 0) 
        {

            int StackLast = stack.Count - 1;

            for (int i =0; i < stack.Count; i++) 
            {
                TwoList[0] = stack[StackLast];

                TwoList[1] = stack[i];

                result.Add(new int[] {TwoList[0], TwoList[1]});
            }
            stack.RemoveAt(StackLast);
        }
        
        return result;

    }

    static int MaxXor(int l, int r) 
    {
            
        List<int> NumList = new List<int>(Enumerable.Range(l, r - l + 1));

        // Inserts the list combinations as int arrays
        List<int[]> ComboList = Combinations(NumList); 

        int max = 0;

        foreach(var two in NumList)
        {
        
            int XorValue = two[0] ^ two[1];

            max = ( max > XorValue )? max : XorValue;
        }
    
        return max;
    }

    static void Main(String[] args) 
    {

        int _l;
        int _r;

        _l = Convert.ToInt32(Console.ReadLine());
         
        _r = Convert.ToInt32(Console.ReadLine());
    
        Console.WriteLine(MaxXor(_l, _r));
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;


class Solution {

    public static List<int[]> Combinations( List<int> number_list ) 
    {
    
        // We are only choosing 2 values out of any list of numbers
        int[] TwoList = new int[2]; 

        List<int[]> result = new List<int[]>();

        List<int> stack = new List<int>(number_list);                    
                
        while (stack.Count > 0) 
        {

            int StackLast = stack.Count - 1;

            for (int i =0; i < stack.Count; i++) 
            {
                TwoList[0] = stack[StackLast];

                TwoList[1] = stack[i];

                result.Add(new int[] {TwoList[0], TwoList[1]});
            }
            stack.RemoveAt(StackLast);
        }
        
        return result;

    }

    static int MaxXor(int l, int r) 
    {
            
        List<int> NumList = new List<int>(Enumerable.Range(l, r - l + 1));

        // Inserts the list combinations as int arrays
        List<int[]> comboList = Combinations(NumList); 

        int max = 0;

        foreach(var two in comboList)
        {
        
            int XorValue = two[0] ^ two[1];

            max = ( max > XorValue )? max : XorValue;
        }
    
        return max;
    }

    static void Main(String[] args) 
    {

        int _l;
        int _r;

        _l = Convert.ToInt32(Console.ReadLine());
         
        _r = Convert.ToInt32(Console.ReadLine());
    
        Console.WriteLine(MaxXor(_l, _r));
    }
}
Rollback to Revision 5
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;


class Solution {

    public static List<int[]> Combinations( List<int> numberListnumber_list ) 
    {
    
        // We are only choosing 2 values out of any list of numbers
        int[] twoListTwoList = new int[2]; 

        List<int[]> result = new List<int[]>();

        List<int> stack = new List<int>(numberListnumber_list);                    
                
        while (stack.Count > 0) 
        {

            int stackLastStackLast = stack.Count - 1;

            for (int i =0; i < stack.Count; i++) 
            {
                twoList[0]TwoList[0] = stack[stackLast];stack[StackLast];

                twoList[1]TwoList[1] = stack[i];

                result.Add(new int[] {twoList[0]TwoList[0], twoList[1]TwoList[1]});
            }
            stack.RemoveAt(stackLastStackLast);
        }
        
        return result;

    }

    static int MaxXor(int l, int r) 
    {
            
        List<int> numListNumList = new List<int>(Enumerable.Range(l, r - l + 1));

        // Inserts the list combinations as int arrays
        List<int[]> comboListComboList = Combinations(numListNumList); 

        int max = 0;

        foreach(var two in numListNumList)
        {
        
            int xorValueXorValue = two[0] ^ two[1];

            max = ( max > xorValueXorValue )? max : xorValue;XorValue;
        }
    
        return max;
    }

    static void Main(String[] args) 
    {

        int _l;
        int _r;

        _l = Convert.ToInt32(Console.ReadLine());
         
        _r = Convert.ToInt32(Console.ReadLine());
    
        Console.WriteLine(MaxXor(_l, _r));
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;


class Solution {

    public static List<int[]> Combinations( List<int> numberList ) 
    {
    
        // We are only choosing 2 values out of any list of numbers
        int[] twoList = new int[2]; 

        List<int[]> result = new List<int[]>();

        List<int> stack = new List<int>(numberList);                    
                
        while (stack.Count > 0) 
        {

            int stackLast = stack.Count - 1;

            for (int i =0; i < stack.Count; i++) 
            {
                twoList[0] = stack[stackLast];

                twoList[1] = stack[i];

                result.Add(new int[] {twoList[0], twoList[1]});
            }
            stack.RemoveAt(stackLast);
        }
        
        return result;

    }

    static int MaxXor(int l, int r) 
    {
            
        List<int> numList = new List<int>(Enumerable.Range(l, r - l + 1));

        // Inserts the list combinations as int arrays
        List<int[]> comboList = Combinations(numList); 

        int max = 0;

        foreach(var two in numList)
        {
        
            int xorValue = two[0] ^ two[1];

            max = ( max > xorValue )? max : xorValue;
        }
    
        return max;
    }

    static void Main(String[] args) 
    {

        int _l;
        int _r;

        _l = Convert.ToInt32(Console.ReadLine());
         
        _r = Convert.ToInt32(Console.ReadLine());
    
        Console.WriteLine(MaxXor(_l, _r));
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;


class Solution {

    public static List<int[]> Combinations( List<int> number_list ) 
    {
    
        // We are only choosing 2 values out of any list of numbers
        int[] TwoList = new int[2]; 

        List<int[]> result = new List<int[]>();

        List<int> stack = new List<int>(number_list);                    
                
        while (stack.Count > 0) 
        {

            int StackLast = stack.Count - 1;

            for (int i =0; i < stack.Count; i++) 
            {
                TwoList[0] = stack[StackLast];

                TwoList[1] = stack[i];

                result.Add(new int[] {TwoList[0], TwoList[1]});
            }
            stack.RemoveAt(StackLast);
        }
        
        return result;

    }

    static int MaxXor(int l, int r) 
    {
            
        List<int> NumList = new List<int>(Enumerable.Range(l, r - l + 1));

        // Inserts the list combinations as int arrays
        List<int[]> ComboList = Combinations(NumList); 

        int max = 0;

        foreach(var two in NumList)
        {
        
            int XorValue = two[0] ^ two[1];

            max = ( max > XorValue )? max : XorValue;
        }
    
        return max;
    }

    static void Main(String[] args) 
    {

        int _l;
        int _r;

        _l = Convert.ToInt32(Console.ReadLine());
         
        _r = Convert.ToInt32(Console.ReadLine());
    
        Console.WriteLine(MaxXor(_l, _r));
    }
}
Added challenge prompt in question, C#'ed some of the syntax,
Source Link
Loading
Added challenge prompt in question, C#'ed some of the syntax
Source Link
Loading
Fixed edit clash
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
fixed copy paste error and changed the title and added tags
Source Link
Malachi
  • 28.7k
  • 11
  • 86
  • 188
Loading
added 51 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
Source Link
Loading