Sort by

recency

|

205 Discussions

|

  • + 0 comments
    # greedy, needs to be compiled to pass the time limit with PyPy3
    def andProduct(a, b):
        return reduce(lambda x, count: count & x, range(a, b+1, (a%2==0)+1))
    
    def andProduct(a, b):
        differing_position = (a ^ b).bit_length()
        
        mask = ~((1 << differing_position) - 1)
        
        return a & mask
    

    The same idea as the above but handling the binary representation as a string throws a runtime error on test-case 3, however, I can't understand what is wrong

        position = b.bit_length() - (a^b).bit_length()
    
        return int(bin(a)[2:][:position] + '0'*(a.bit_length() - position), 2)
    
  • + 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
    }