# Longest Common Subsequence with at most k changes allowed

Given two sequence **P** and **Q** of numbers. The task is to find Longest Common Subsequence of two sequence if we are allowed to change at most **k** element in first sequence to any value.

Examples:

Input : P = { 8, 3 } Q = { 1, 3 } K = 1 Output : 2 If we change first element of first sequence from 8 to 1, both sequences become same. Input : P = { 1, 2, 3, 4, 5 } Q = { 5, 3, 1, 4, 2 } K = 1 Output : 3 By changing first element of first sequence to 5 to get the LCS ( 5, 3, 4 }.

The idea is to use Dynamic Programming. Define a 3D matrix dp[][][], where dp[i][j][k] defines the Longest Common Subsequence for the first i numbers of first array, first j number of second array when we are allowed to change at max k number in the first array.

Therefore, recursion will look like

If P[i] != Q[j], dp[i][j][k] = max(dp[i - 1][j][k], dp[i][j - 1][k], dp[i - 1][j - 1][k - 1] + 1) If P[i] == Q[j], dp[i][j][k] = max(dp[i - 1][j][k], dp[i][j - 1][k], dp[i - 1][j - 1][k] + 1)

Below is C++ implementation of this approach:

`// CPP program to find LCS of two arrays with ` `// k changes allowed in first array. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define MAX 10 ` ` ` `// Return LCS with at most k changes allowed. ` `int` `lcs(` `int` `dp[MAX][MAX][MAX], ` `int` `arr1[], ` `int` `n, ` ` ` `int` `arr2[], ` `int` `m, ` `int` `k) ` `{ ` ` ` `// If at most changes is less than 0. ` ` ` `if` `(k < 0) ` ` ` `return` `-1e7; ` ` ` ` ` `// If any of two array is over. ` ` ` `if` `(n < 0 || m < 0) ` ` ` `return` `0; ` ` ` ` ` `// Making a reference variable to dp[n][m][k] ` ` ` `int` `& ans = dp[n][m][k]; ` ` ` ` ` `// If value is already calculated, return ` ` ` `// that value. ` ` ` `if` `(ans != -1) ` ` ` `return` `ans; ` ` ` ` ` `// calculating LCS with no changes made. ` ` ` `ans = max(lcs(dp, arr1, n - 1, arr2, m, k), ` ` ` `lcs(dp, arr1, n, arr2, m - 1, k)); ` ` ` ` ` `// calculating LCS when array element are same. ` ` ` `if` `(arr1[n] == arr2[m]) ` ` ` `ans = max(ans, 1 + lcs(dp, arr1, n - 1, ` ` ` `arr2, m - 1, k)); ` ` ` ` ` `// calculating LCS with changes made. ` ` ` `ans = max(ans, 1 + lcs(dp, arr1, n - 1, ` ` ` `arr2, m - 1, k - 1)); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `k = 1; ` ` ` `int` `arr1[] = { 1, 2, 3, 4, 5 }; ` ` ` `int` `arr2[] = { 5, 3, 1, 4, 2 }; ` ` ` `int` `n = ` `sizeof` `(arr1) / ` `sizeof` `(arr1[0]); ` ` ` `int` `m = ` `sizeof` `(arr2) / ` `sizeof` `(arr2[0]); ` ` ` ` ` `int` `dp[MAX][MAX][MAX]; ` ` ` `memset` `(dp, -1, ` `sizeof` `(dp)); ` ` ` ` ` `cout << lcs(dp, arr1, n, arr2, m, k) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

3

**Time Complexity: **O(N*M*K).

This article is contributed by **Anuj Chauhan**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Longest common subsequence with permutations allowed
- Longest Common Subsequence | DP-4
- LCS (Longest Common Subsequence) of three strings
- Printing Longest Common Subsequence
- Longest Common Increasing Subsequence (LCS + LIS)
- Longest Common Subsequence | DP using Memoization
- C++ Program for Longest Common Subsequence
- Java Program for Longest Common Subsequence
- Length of longest common subsequence containing vowels
- Edit distance and LCS (Longest Common Subsequence)
- Python Program for Longest Common Subsequence
- Minimum cost to make Longest Common Subsequence of length k
- Printing Longest Common Subsequence | Set 2 (Printing All)
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Count common subsequence in two strings