• + 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)