Why the code is giving incorrect output?

#include
using namespace std;
int count(int n,int m)
{

//Base Case
if(n==1 || n==2)
{
	return n;
}
//Recursive Case
return count(n,n-1)+count(n,n-m);

}
int main() {
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n,m;
cin>>n>>m;
cout<<count(n,m)<<endl;
}
return 0;
}

You need to memoise this. As it is of exponential complexity. Memoisation would allow to make it m*n complexity and also your base case is incorrect. It will lead to stack overflow error, as it never terminates since ‘n’ remains the same.

//Base Case 	if(n==1) 	{ 		return 1; 	} 	if(n==4) 	{ 		return 2; 	}

How can I make that Memoise?

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.