Sort by

recency

|

150 Discussions

|

  • + 0 comments

    Haskell

    So, if you look at the sequence of running XORs over the XORs, there's a pattern. I didn't give it much thought after that -- just implement the lookup function.

    module Main where
    
    import Control.Monad (replicateM_)
    import Data.Bits (xor)
    
    lu :: Int -> Integer
    lu 0 = 1
    lu 1 = 2
    lu 2 = 2
    lu x = do
        let (q, r) = (x - 3) `quotRem` 8
            b = 8 * (fromIntegral q) + 6 :: Integer
         in [b, b + 1, 0, 0, b + 2, b + 3, 2, 2] !! r
    
    solve :: Int -> Int -> Integer
    solve 1 b = lu (b - 1)
    solve a b = xor (lu (a - 2)) (lu (b - 1))
    
    main :: IO ()
    main = do
        cases <- readLn
        replicateM_ cases $ do
            [a, b] <- map read . words <$> getLine
            print $ solve a b
    
  • + 0 comments

    Hi there! Here is my Java solution for the challenge. If you'd like to discuss programming or any other topics, don't hesitate to reach out!

    https://github.com/eduardocintra/hacker-rank-solutions/blob/master/src/br/com/eduardocintra/medium/xorsequence/XorSequence.java

  • + 0 comments

    c++ optimal one

    long getXor(long x){
        int n=x%8;
        if(n==0||n==1)return x;
        if(n==2||n==3)return 2;
        if(n==4||n==5)return x+2;
        else return 0;
    }
    
    long xorSequence(long l, long r) {
    return getXor(r)^getXor(l-1);
    }
    
  • + 0 comments

    where is an array to search or to perform a function

  • + 1 comment

    like zaber04 said, we need to set up an array starting from 0 to a certain number to see the pattern. The following is the code I use to set up the array and observe the pattern:

    result = 0
    further_xor = 0
    print(0, "\t", result)
    for i in range(1, 40):
      result = result ^ i
      further_xor = further_xor ^ result
    
      print(i, "\t", result, "\t", further_xor)