0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

can you explain more
It's just that the value of Z that you're looking for will be at least x|y try it out on a piece of paper with bit representation of numbers that you'll take as x and y. Example x=600 y=900, now If you try brute forcing between a range say (0,2000). You'll get the value as 988. which is basically x|y which will get 600&988=600 and 900&988=900
but it needs to be in this range
That you should do it on your own

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.