Please help finding error as 2 test cases go wrong

#include<bits/stdc++.h>
using namespace std;
int main() {
long long n,m,x,y;
cin>>n>>m>>x>>y;
long long res=m/x;
m=m%x;
if(res>=n)
cout<<n<<endl;
else
{
n=n-res;
long long i=0;
while(i+1<n)
{
m+=y;
n–;
if(m>=x)
{
long long j=m/x;
if(i+j!=n+1)
{
res=res+(m/x);
i=i+(m/x);
}
else
{
res=res+(m/x)-1;
i=i+(m/x);
break;
}
m=m%x;
}
}
cout<<res<<endl;
}

return 0;

}

please use the binary search approach to solve this question

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.