CHANDF - EDITORIAL

Yes, there are can be multiple approaches. But this is a basic approach to this problem.
There are even beautiful recursive approaches to this problem.
You may have a look at them too.
xuanyiming’s solution
ei_captain’s solution

2 Likes

Hey I tried implementing the solution using your approach.
But i am getting Runtime-error , it is mostly because of overflow because if i remove

#pragma GCC optimize “trapv” // abort() on (signed) integer overflow.

this line from my code, it shows WA.

Submission
Can anyone hint why? :frowning:

Wow, they look so simple. orz :open_mouth:

You have to optimize your solution and find an approach for traversing bits and solving it.
You cannot loop from L to R.

1 Like

Hardifying problems: In the initial proposal there was some weird constraints with the intention of making the answer always equal to X&Y. After adding the bounds L,R for Z, even the setter had troubles solving the problem :slight_smile:

10 Likes

Great question @harshil21!! :fire:

This is a kind of question that seems easy when you read it for the first time and encourages more and more users to participate. Very creatively designed question. Looking forward to such questions!

10 Likes

Try this test case:

1
1133953 63182 0 595

Your answer -> 1179203
Expected answer -> 463

1 Like

Damn my answer is no where close.

Nice Editorial!!

1 Like

Let’s represent the numbers as binary strings, and denote the iii-th most significant bit of number S by S[i]. For example the string (binary) representation of 18 is S=10010, note that S[3]=1.

This is quoted from the NOTATION part para 2. Can you please explain why S[3] = 1?

the indexing started from 0 like in arrays

1 Like

I tried to solve the subtask, but don’t know what went wrong .
x|y
https://www.codechef.com/viewsolution/32692655
https://www.codechef.com/viewsolution/32692466

Am I doing anything other than x|y.

1 Like

For the first sub task, i thought the same but it produced wrong result.
Here is my solution:
https://www.codechef.com/viewsolution/32933172
Please help.

Basically Our goal in this problem is to preserve maximum the set bits for both of and terms while keeping the value in the range L to R…We can hard code the logic to which bit can be set such that it maximizes the product and is the smallest wouldn’t that work?

See my Implementation:: CodeChef: Practical coding for everyone

1 Like

same thing happened with me too.

“Common error”
Try X = 0 and Y = 3 and L = 0, R = 10?
Answer should be 0.

2 Likes

@reena_code_123 There is a corner case when x or y is zero. Because then no matter whichever z you choose value of the function stays zero. so minimum value of z in that case would be 0. This is under common errors. Did the same mistake :grin:

Beware of cases where X,Y,L,R = 0X,Y,L,R=0.

1 Like

Time to die…“Ohh bhai maro …mujhe maroo :sweat_smile:
I attempted several times …also in python but didn’t thaught of
R=0;

Checking for R=0??

@harshil21 @krishna_kc Don’t see the solution, i got it :sweat_smile::sweat_smile::sweat_smile:, Mistakely, I added return their.:sweat_smile:

2 Likes