please have a glance on my code and please tell whats wrong there.
Xor profit problem
Please share your code link.
this code is just for testing purpose the actual code requires only two input values
new link of the code
Why are you running the loop for subsets? Did you send the correct code?
so for approaching this question i decided to calculate all the subsets in the range and then the subsets with pair of two are extracted and then the value be assigned to variable ar and br and the xor between all the extracted pairs are calculated and compared
please tell if the approach is wrong or right
You cannot calculate all the subsets int the range, so your approach is incorrect.
can you please explain me the solution
A simple solution is to generate all pairs, find their XOR values and finally return the maximum XOR value.
An efficient solution is to consider pattern of binary values from L to R. We can see that first bit from L to R either changes from 0 to 1 or it stays 1 i.e. if we take the XOR of any two numbers for maximum value their first bit will be fixed which will be same as first bit of XOR of L and R itself. After observing the technique to get first bit, we can see that if we XOR L and R, the most significant bit of this XOR will tell us the maximum value we can achieve i.e. let XOR of L and R is 1xxx where x can be 0 or 1 then maximum XOR value we can get is 1111 because from L to R we have all possible combination of xxx and it is always possible to choose these bits in such a way from two numbers such that their XOR becomes all 1.
please refer this code i have come up with new solution but all testcases not passing;
sample testcase was passed.
#include
using namespace std;
int main () {
int a,b;
cin>>a>>b;
int xorr=a^b;
int cnt=0;
//find position
while(xorr)
{
cnt++;
xorr=xorr>>1;
}
int ans=(~(-1<<cnt));
cout<<ans;
return 0;
}