int min_steps(int target, int move)
{
int x,y,z;
cout<<target<<" "<<move<<endl;
if(target==move || target+move==0)
{
return 1;
}
x = 1 + min_steps(target-move,move+1);
y = 1 + min_steps(target+move,move+1);
z = x<y?x:y;
return z;
}
int main() {
cout<<min_steps(3,1);
return 0;
}
In the above recursive function min_steps
, the cout
statement has been included to track the recursive calls. Now min_steps(3,1)
encounters a call where target=2
& move=2
, in which case the if condition
holds True
& therefore the function is supposed to return 1 & break. But this is not happening. The function is continuing to make calls and thus resulting in Time limit exceeded
error