Sort by

recency

|

207 Discussions

|

  • + 0 comments

    Here is my Python solution!

    def andProduct(a, b):
        a = bin(a)[2:]
        b = bin(b)[2:]
        if len(a) != len(b):
            return 0
        for i in range(len(a)):
            if a[i] != b[i]:
                return int(a[:i] + (len(a) - i) * "0", 2)
        return int(a, 2)
    
  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while(n--!=0){
            long a=sc.nextLong();
            long b=sc.nextLong();
            long and=a;
            if(a%4==0){
                for(long i=a+4;i<=b;i+=4){
                    and&=i;
                }
            }
            else if(a%2==0){
                for(long i=a+2;i<=b;i+=2){
                    and&=i;
                }
            }
            else{
                for(long i=a+1;i<=b;i++){
                    and&=i;
                }
            }
            System.out.println(and);
        }
    }
    

    }

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