Help to reduce the time taken

My issue

My code

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;


int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    int n;
	    cin>>n;
	    vector<int> rem;
	    int i=1;
	    while(n--){
	        int x = n%i;
	        rem.push_back(x);
	        i++;
	    }
    int max=*max_element(rem.begin(),rem.end());
	int j=rem.size()-1;
    while(rem[j]!=max){
        j--;
    }
    cout<<j+1<<endl;
	}
	return 0;
}

Problem Link: MUFFINS3 Problem - CodeChef

@abhinavn
I didn’t go thro your code thoroughly but i guess you are trying to reach the output using loop.
I guess this question is just a bit maths
I have pasted my code below .
hope this helps!!

include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t–)
{
long long n;
cin>>n;
cout<<n / 2 + 1<<endl;
}

return 0;

}