Please check code for K-ordered LCS


Not able to pass 1 test case

hello @sahilkhan2312000131
u cannot do like this.

the logic will fail for cases such as->
5 5 1
1 2 3 4 5
5 1 2 3 4
hint -> try to modify ur lcs recurrence using k

not able to build logic… please help

see logic is similar to lcs only .

suppose we have s1and s2 as string and k.
i=0 (for string s1)
j=0(for string s2)

if s1[i]==s2[j]
then ans=1+solvefor(i+1,j+1,k)
if(s1[i]!=s2[j] (we have two options)

  ans=solvefor(i+1,j,k) or solvefor(i,j+1,k) take maximum among them // we did similar thing in lcs
  if k>0 then i can use 1 k to make them equal and lcs in that case will be

     ans=1+solvefor(i+1,j+1,k-1) // k-1 because i have used 1 k to make them equal

among these two options whichever will give maximum answe we will consider that.

for optimisation use 3d dp table

code->

but i think my code works fine for this test case as it outputs:5 for this test case.

correct output is 4.
1 2 3 4 5
5 1 2 3 4

by no means we can make it 5

your code is giving seg fault

use int for dp table , , ,

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.