ArJun Gawali
ArJun Gawali

Reputation: 47

Fastest way to solve any python program containing bitwise operator

I have a program in which I need to find the maximum value of expression.
The expression is :

result=(x&z)*(y&z)

in which the value of z is in the range of [l,r].
I have written a program about it which is absolutely correct.

# cook your dish here
t=int(input())

for i in range(t):
    x,y,l,r=map(int,input().split())
    L=[]
    I=[]
    for i in range(l,r+1):
        r=(x&i)*(y&i)
        L.append(r)
        I.append(i)
    R=max(L)
    i=L.index(R)
    print(L)
    print(I[i])

I want to a faster way to do it

Upvotes: 0

Views: 181

Answers (1)

Tanuj Nagpal
Tanuj Nagpal

Reputation: 58

Your result will be maximized when the product is of the numbers x*y itself as A&B can't exceed min(A,B), Whatever the range l,r+1 might be. in this case x|y will give a number with which doing AND with either x or y will return the same number

Upvotes: 1

Related Questions