My issue
i have tried solving it using iterative approach and the code runs fine , but I get a issue regarding time limit exceeding. Any idea how to optimize it and what topic to study to face these issue ?
My code
#include <iostream>
using namespace std;
int main() {
int t,n;
cin>>t;
for (int i = 0; i < t; i++) {
cin>>n;
int remaining_cupcakes=0;
int value_of_a=1;
for (int a = 1; a <=n; a++) {
int no_of_packages=n/a;
int curr_remaining_cupcakes=n-(a*no_of_packages);
if(remaining_cupcakes<curr_remaining_cupcakes){
remaining_cupcakes=curr_remaining_cupcakes;
value_of_a=a;
}
}
if(remaining_cupcakes==0)
value_of_a=n;
cout<<value_of_a<<endl;
}
return 0;
}
Problem Link: Packaging Cupcakes Practice Coding Problem - CodeChef