hi i recently solved bytelandian problem(link below)
my solution(below) which exeeds the time limit
using namespace std;
long long int fun(long long int n)
{
long long int a=(n/3)+(n/4)+(n/2);
if(a>n)
return (fun(n/3)+fun(n/4)+fun(n/2));
return (n);
}
int main()
{
long long int n;
while(cin>>n)
{
cout<<fun(n)<<"\n";
}
return 0;
}
ideal solution
#include
#include
#include
#include
using namespace std;
long long int fun(long long int n,long long int *m)
{
if(n==0 || n==1)
return n;
if(m[n]!=0)
return m[n];
m[n]=max(n,fun(n/2,m)+fun(n/3,m)+fun(n/4,m));
return m[n];
}
int main() {
long long int n;
while(cin>>n)
{
long long int *m=new long long int[n+1];
cout<<fun(n,m)<<“\n”;
}
// your code goes here
return 0;
}
can you plz tell me why my ans is taking more time