Skip to main content
3 of 9
fixed copy paste error and changed the title and added tags
Malachi
  • 28.7k
  • 11
  • 86
  • 188

Maximizing XOR HackerRank challenge

So I know that there are combinatoric libraries for this kind of thing. However, with me being new to C# and coding in general I found that I couldn't understand the code well enough to implement it in my solution.

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

Code below

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;


class Solution {

// Process the combinations
    public static List<int[]> Combinations( List<int> number_list ) {

    
        // We are only choosing 2 values out of any list of numbers
        int[] two_list = new int[2]; 

        List<int[]> result = new List<int[]>(); // This will be a list of 2 Tuples

        // comes from our number list parameter
        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++) {

                two_list[0] = stack[stackLast];

                two_list[1] = stack[i];

                result.Add(new int[] {two_list[0], two_list[1]});

            }
            stack.RemoveAt(stackLast);
        }
        
        return result;

    }

    static int maxXor(int l, int r) {
                
        List<int> num_list = new List<int>(Enumerable.Range(l, r - l + 1)); // Voodoo lol

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

        /*********************find max Xor**********************/
        int max = 0;

        foreach(var two in combo_list){
            
            int xor_value = two[0] ^ two[1];

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

    static void Main(String[] args) {

        //int res;
        int _l;
        int _r;

        _l = Convert.ToInt32(Console.ReadLine());
            
        _r = Convert.ToInt32(Console.ReadLine());
        
        //res = maxXor(_l, _r);
    
        Console.WriteLine(maxXor(_l, _r));
    
    }
}