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
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?
Wow, they look so simple. orz
You have to optimize your solution and find an approach for traversing bits and solving it.
You cannot loop from L to R.
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
Great question @harshil21!!
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!
Try this test case:
1
1133953 63182 0 595
Your answer -> 1179203
Expected answer -> 463
Damn my answer is no where close.
Nice Editorial!!
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
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.
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
same thing happened with me too.
“Common error”
Try X = 0 and Y = 3 and L = 0, R = 10?
Answer should be 0.
@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
Beware of cases where X,Y,L,R = 0X,Y,L,R=0.
Time to die…“Ohh bhai maro …mujhe maroo ”
I attempted several times …also in python but didn’t thaught of
R=0;
Checking for R=0??