My Solution to the problem Greedy Puppy (Code : GDOG) is wrong, could anyone please help me with it?

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

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++)
            q = (n/j)+1;
            r = n%q;
    cout << r << endl;

Your code doesn’t work in Test case: 37 6

Your answer = 1.

Correct answer = 2.

Iterate over all the values of K, it’d stil be O(K) which will work.

Thanks dude, got it ryt now