what is the approach to this problem?
Why are we using 3d array?
You need to use the following approach as :
if(a[i]==b[j])
{
res=1+lcs_with_k_ordered(i+1,j+1,k);
}
else
{
if(k>0)
{
res=1+lcs_with_k_ordered(i+1,j+1,k-1);
}
res=max(res,lcs_with_k_ordered(i,j+1,k));
res=max(res,lcs_with_k_ordered(i+1,j,k));
}
why are we doing k-1 in a case k in rest
This is because if your both the values in string doesn’t match, then in that case, firstly you will check if k is still greater than zero, then to match both the values, we will decrement the value of k and hence in the parameter you will pass k-1.