My code is written here, and my logic to this problem is that when k is greater than integer part of n/j but lesser than integer part of n/(j-1), where j is a natural number; then maximum remainder is obtained if the divisor is smallest, ie, one added to integer part of n/j. However, I can’t find my error here. Can anyone please help me with this? GDOG
#include<iostream>
using namespace std;
int main()
{
int t;
cin >> t;
for(int i=0; i<t; i++)
{
int n,k;
cin >> n >> k;
int q,r;
for(int j=1; j<=n+1; j++)
{
if(k>n/j)
{
q = (n/j)+1;
r = n%q;
break;
}
}
cout << r << endl;
}
}