#include <iostream>
#include <algorithm>
using namespace std;
int maxXor(int l, int r)
{
int i,j,m,ans=0;
for(i=l;i<r;i++)
{
for(j=i+1;j<=r;j++)
{
m=i^j;
if(ans<=m)
ans=m;
}
}
return ans;
}
int main()
{
int res;
int _l;
cin >> _l;
int _r;
cin >> _r;
res = maxXor(_l, _r);
cout << res;
return 0;
}
#include <algorithm>
using namespace std;
int maxXor(int l, int r)
{
int i,j,m,ans=0;
for(i=l;i<r;i++)
{
for(j=i+1;j<=r;j++)
{
m=i^j;
if(ans<=m)
ans=m;
}
}
return ans;
}
int main()
{
int res;
int _l;
cin >> _l;
int _r;
cin >> _r;
res = maxXor(_l, _r);
cout << res;
return 0;
}
The solution for each pair of (l,r) takes O(n^2) time. Can you please post a solution with a lower time complexity ? Thanks in advance
ReplyDelete