Sort by

recency

|

204 Discussions

|

  • + 0 comments

    The question is missing edge cases where a=b and a

  • + 0 comments

    The question is missing edge cases where a=b and a

  • + 0 comments

    Haskell

    I'm kind of surprised to see implementations that AND over the list in the discussion; a Haskell foldl1 (.&.) [a..b] (basically the same thing) would often timeout.

    module Main where
    
    import Data.Bits (shiftL, shiftR, (.&.))
    import Control.Monad (replicateM_)
    
    solve :: [Integer] -> Integer
    solve [a, b] = commonPrefix a b
      where
        commonPrefix x y
            | x == y = x
            | otherwise = commonPrefix (x `shiftR` 1) (y `shiftR` 1) `shiftL` 1
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_ cases $ do
            getLine >>= print . solve . map read . words
    
  • + 0 comments

    simplicity at its finest in Go

    func andProduct(a int64, b int64) int64 {
        res := a
        for i := a+1;i <= b;i++ {
            res = res & i
        }
        
        return res
    }
    
  • + 0 comments

    Java

    public static long andProduct(long a, long b) {
    
      long result=a;
          for(long i=a+1;i<=b;i++){
              result=result & i;
          }
          return result;
    }