Help me in solving MUFFINS3 problem

My issue

It is Showing time limit exceeded.
pls tell me a simple solution i can use to solve this!

My code

#include <iostream>
#include <vector>
#include <string> 
using namespace std;
void solve(){
    int n;
    cin>>n;
    int ans{0};
    int max{0};
    if(n%2==0){
        ans=n;
    }   
    else if(n%2!=0){
        for(int i=1;i<=n;i++){
            if(n%i>max){
                max=n%i;
                ans=i;
            }
        }
    }
    cout<<ans<<endl;
}
int main(){
    int n;
    cin>>n;
    while(n--){
        solve();
    }
}

Problem Link: MUFFINS3 Problem - CodeChef

If we want Chef to eat the maximum number of leftover cupcakes, we must give him 1 more than half of the total cupcakes, otherwise he can make another package.

In code:

#include <iostream>
using namespace std;

void solve() {
    int n; cin >> n;
    cout << n / 2 + 1 << '\n';
}

int main() {
	int t; cin >> t;
	while (t--) solve();
	return 0;
}
1 Like