Xor profit problem

this is my code works properly. is there any other method to do it.

#include
using namespace std;

int profit(int x, int y)
{
int a=x;
int b=y;
int i,ans=0;
int max=0;
for(i=0;i<=(y-x);i++)
{
ans=a^b;
if(max<ans)
max=ans;
a++;
}
for(i=0;i<=(y-x);i++)
{
ans=a^b;
if(max<ans)
max=ans;
b–;
}

return max;

}
int main () {
int x,y,ans;
cin>>x>>y;
ans=profit(x,y);
cout<<ans;
return 0;
}

Hey @Sajid07 you can refer the following link for a better approach for this problem. Hope this helps

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.