Maximizing XOR Discussions | Algorithms | HackerRank

Sort by

recency

|

489 Discussions

|

  • + 0 comments

    Here is my easy and straitghforward solution, you can watch video here : https://youtu.be/pCU8W_ofvDg

    int maximizingXor(int l, int r) {
        int result = 0;
        for(int a = l; a <= r; a++){
            for(int b = a; b<= r; b++){
                result = max(result, a ^ b);
            }
        }
        return result;
    }
    
  • + 0 comments

    Haskell

    module Main where
    
    import Data.Bits (xor)
    
    solve :: Int -> Int -> Int
    solve l r = maximum [a `xor` b | a <- [l .. r - 1], b <- [a .. r]]
    
    main :: IO ()
    main = do
        l <- readLn :: IO Int
        r <- readLn :: IO Int
        print $ solve l r
    
  • + 0 comments

    ![]( def maximizingXor(l, r): new_results = 0 for i in range(l, r + 1): for j in range(l,r + 1): old_result = i ^ j if old_result >= new_results: new_results = old_result return new_resultshttps://)

  • + 0 comments
    from operator import xor
    def maximizingXor(l, r):
        # Write your code here
        if l == r:
            return 0
        maxVal = 0
        a = l
        b = a + 1
        while b <= r:
            maxVal = max(maxVal, xor(a, b))
            b += 1
            a += 1
        return maxVal
    
  • + 0 comments

    Python, one-liner:

    return 2**(1+int(math.floor(math.log(l^r, 2.0))))-1