Minimize elements to be added to a given array such that it contains another given array as its subsequence

Given an array A[] consisting of N distinct integers and another array B[] consisting of M integers, the task is to find the minimum number of elements to be added to the array B[] such that the array A[] becomes the subsequence of the array B[].
Examples:
Input: N = 5, M = 6, A[] = {1, 2, 3, 4, 5}, B[] = {2, 5, 6, 4, 9, 12}Ā
Output: 3
Explanation:
Below are the element that are needed to be added:
1) Add 1 before element 2 of B[]2) Add 3 after element 6 of B[]3) Add 5 in the last position of B[].
Therefore, the resulting array B[] is {1, 2, 5, 6, 3, 4, 9, 12, 5}.
Hence, A[] is the subsequence of B[] after adding 3 elements.Input: N = 5, M = 5, A[] = {3, 4, 5, 2, 7}, B[] = {3, 4, 7, 9, 2}Ā
Output: 2Ā
Explanation:Ā
Below are the elements that are needed to be added:Ā
1) Add 5 after element 4.Ā
2) Add 2 after element 5.Ā
Therefore, the resulting array B[] is {3, 4, 5, 2, 7, 9, 2}.Ā
Hence 2 elements are required to be added.
Naive Approach: The naive approach is to generate all the subsequences of the array B and then find that subsequence such that on adding a minimum number of elements from the array A to make it equal to the array A. Print the minimum count of element added.
Time Complexity: O(N*2M)
Auxiliary Space: O(M+N)Ā
Efficient Approach: The above approach can be optimized using Dynamic Programming. The idea is to find the Longest Common Subsequence between the given two arrays A and B. The main observation is that the minimum number of elements to be added in B[] such that A[] becomes its subsequence can be found by subtracting the length of the longest common subsequence from the length of the array A[].
Therefore, the difference between the length of the array A[] and length of the Longest Common Subsequence is the required result.
Below is the implementation of the above approach:
C++14
// C++14 program for the above approach#include <bits/stdc++.h>using namespace std;Ā
// Function that finds the minimum number// of the element must be added to make A// as a subsequence in Bint transformSubsequence(int n, int m,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vector<int> A,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vector<int> B){Ā Ā Ā Ā Ā Ā Ā Ā Ā // Base CaseĀ Ā Ā Ā if (B.size() == 0)Ā Ā Ā Ā Ā Ā Ā Ā return n;Ā
Ā Ā Ā Ā // dp[i][j] indicates the length ofĀ Ā Ā Ā // LCS of A of length i & B of length jĀ Ā Ā Ā vector<vector<int>> dp(n + 1, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vector<int>(m + 1, 0));Ā
Ā Ā Ā Ā for(int i = 0; i < n + 1; i++)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā for(int j = 0; j < m + 1; j++)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If there are no elementsĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // either in A or B then theĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // length of lcs is 0Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == 0 or j == 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = 0;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the element present atĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // ith and jth index of A and BĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // are equal then include in LCSĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (A[i - 1] == B[j - 1])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = 1 + dp[i - 1][j - 1];Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If they are not equal thenĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // take the maxĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = max(dp[i - 1][j],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j - 1]);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Return difference of lengthĀ Ā Ā Ā // of A and lcs of A and BĀ Ā Ā Ā return n - dp[n][m];}Ā
// Driver Codeint main(){Ā Ā Ā Ā int N = 5;Ā Ā Ā Ā int M = 6;Ā
Ā Ā Ā Ā // Given sequence A and BĀ Ā Ā Ā vector<int> A = { 1, 2, 3, 4, 5 };Ā Ā Ā Ā vector<int> B = { 2, 5, 6, 4, 9, 12 };Ā
Ā Ā Ā Ā // Function callĀ Ā Ā Ā cout << transformSubsequence(N, M, A, B);Ā
Ā Ā Ā Ā return 0;}Ā
// This code is contributed by mohit kumar 29 |
Java
// Java program for // the above approachimport java.util.*;class GFG{Ā
// Function that finds the minimum number// of the element must be added to make A// as a subsequence in Bstatic int transformSubsequence(int n, int m,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int []A, int []B){Ā Ā // Base CaseĀ Ā if (B.length == 0)Ā Ā Ā Ā return n;Ā
Ā Ā // dp[i][j] indicates the length ofĀ Ā // LCS of A of length i & B of length jĀ Ā int [][]dp = new int[n + 1][m + 1];Ā
Ā Ā for(int i = 0; i < n + 1; i++)Ā Ā {Ā Ā Ā Ā for(int j = 0; j < m + 1; j++)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā // If there are no elementsĀ Ā Ā Ā Ā Ā // either in A or B then theĀ Ā Ā Ā Ā Ā // length of lcs is 0Ā Ā Ā Ā Ā Ā if (i == 0 || j == 0)Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = 0;Ā
Ā Ā Ā Ā Ā Ā // If the element present atĀ Ā Ā Ā Ā Ā // ith and jth index of A and BĀ Ā Ā Ā Ā Ā // are equal then include in LCSĀ Ā Ā Ā Ā Ā else if (A[i - 1] == B[j - 1])Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = 1 + dp[i - 1][j - 1];Ā
Ā Ā Ā Ā Ā Ā // If they are not equal thenĀ Ā Ā Ā Ā Ā // take the maxĀ Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā dp[i][j] = Math.max(dp[i - 1][j],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j - 1]);Ā Ā Ā Ā }Ā Ā }Ā
Ā Ā // Return difference of lengthĀ Ā // of A and lcs of A and BĀ Ā return n - dp[n][m];}Ā
// Driver Codepublic static void main(String[] args){Ā Ā int N = 5;Ā Ā int M = 6;Ā
Ā Ā // Given sequence A and BĀ Ā int []A = {1, 2, 3, 4, 5};Ā Ā int []B = {2, 5, 6, 4, 9, 12};Ā
Ā Ā // Function callĀ Ā System.out.print(transformSubsequence(N, M, A, B));}}Ā
// This code is contributed by 29AjayKumar |
Python3
# Python3 program for the above approachĀ
# Function that finds the minimum number# of the element must be added to make A# as a subsequence in Bdef transformSubsequence(n, m, A, B):Ā
Ā Ā Ā Ā # Base CaseĀ Ā Ā Ā if B is None or len(B) == 0:Ā Ā Ā Ā Ā Ā Ā Ā return nĀ
Ā Ā Ā Ā # dp[i][j] indicates the length ofĀ Ā Ā Ā # LCS of A of length i & B of length jĀ Ā Ā Ā dp = [[0 for col in range(m + 1)]Ā Ā Ā Ā Ā Ā Ā Ā for row in range(n + 1)]Ā
Ā Ā Ā Ā for i in range(n + 1):Ā
Ā Ā Ā Ā Ā Ā Ā Ā for j in range(m + 1):Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If there are no elementsĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # either in A or B then theĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # length of lcs is 0Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if i == 0 or j == 0:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = 0Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If the element present atĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # ith and jth index of A and BĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # are equal then include in LCSĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elif A[i-1] == B[j-1]:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = 1 + dp[i-1][j-1]Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If they are not equal thenĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # take the maxĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = max(dp[i-1][j], dp[i][j-1])Ā
Ā Ā Ā Ā # Return difference of lengthĀ Ā Ā Ā # of A and lcs of A and BĀ Ā Ā Ā return n - dp[n][m]Ā
Ā
# Driver Codeif __name__ == "__main__":Ā
Ā Ā Ā Ā N = 5Ā Ā Ā Ā M = 6Ā Ā Ā Ā Ā Ā Ā Ā Ā # Given Sequence A and BĀ Ā Ā Ā A = [1, 2, 3, 4, 5]Ā Ā Ā Ā B = [2, 5, 6, 4, 9, 12]Ā
Ā Ā Ā Ā # Function CallĀ Ā Ā Ā print(transformSubsequence(N, M, A, B)) |
C#
// C# program for // the above approachusing System;class GFG{Ā
// Function that finds the minimum number// of the element must be added to make A// as a subsequence in Bstatic int transformSubsequence(int n, int m,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int []A, int []B){Ā Ā // Base CaseĀ Ā if (B.Length == 0)Ā Ā Ā Ā return n;Ā
Ā Ā // dp[i,j] indicates the length ofĀ Ā // LCS of A of length i & B of length jĀ Ā int [,]dp = new int[n + 1, m + 1];Ā
Ā Ā for(int i = 0; i < n + 1; i++)Ā Ā {Ā Ā Ā Ā for(int j = 0; j < m + 1; j++)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā // If there are no elementsĀ Ā Ā Ā Ā Ā // either in A or B then theĀ Ā Ā Ā Ā Ā // length of lcs is 0Ā Ā Ā Ā Ā Ā if (i == 0 || j == 0)Ā Ā Ā Ā Ā Ā Ā Ā dp[i, j] = 0;Ā
Ā Ā Ā Ā Ā Ā // If the element present atĀ Ā Ā Ā Ā Ā // ith and jth index of A and BĀ Ā Ā Ā Ā Ā // are equal then include in LCSĀ Ā Ā Ā Ā Ā else if (A[i - 1] == B[j - 1])Ā Ā Ā Ā Ā Ā Ā Ā dp[i, j] = 1 + dp[i - 1, j - 1];Ā
Ā Ā Ā Ā Ā Ā // If they are not equal thenĀ Ā Ā Ā Ā Ā // take the maxĀ Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā dp[i, j] = Math.Max(dp[i - 1, j],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i, j - 1]);Ā Ā Ā Ā }Ā Ā }Ā
Ā Ā // Return difference of lengthĀ Ā // of A and lcs of A and BĀ Ā return n - dp[n, m];}Ā
// Driver Codepublic static void Main(String[] args){Ā Ā int N = 5;Ā Ā int M = 6;Ā
Ā Ā // Given sequence A and BĀ Ā int []A = {1, 2, 3, 4, 5};Ā Ā int []B = {2, 5, 6, 4, 9, 12};Ā
Ā Ā // Function callĀ Ā Console.Write(transformSubsequence(N, M, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā A, B));}}Ā
// This code is contributed by Rajput-Ji |
Javascript
<script>Ā
// JavaScript program for the above approachĀ
// Function that finds the minimum number// of the element must be added to make A// as a subsequence in Bfunction transformSubsequence(n, m, A, B){Ā Ā Ā Ā Ā Ā Ā Ā Ā // Base CaseĀ Ā Ā Ā if (B.length == 0)Ā Ā Ā Ā Ā Ā Ā Ā return n;Ā
Ā Ā Ā Ā // dp[i][j] indicates the length ofĀ Ā Ā Ā // LCS of A of length i & B of length jĀ Ā Ā Ā var dp = Array.from(Array(n+1), ()=>Array(m+1).fill(0));Ā
Ā Ā Ā Ā for(var i = 0; i < n + 1; i++)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā for(var j = 0; j < m + 1; j++)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If there are no elementsĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // either in A or B then theĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // length of lcs is 0Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == 0 || j == 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = 0;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the element present atĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // ith and jth index of A and BĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // are equal then include in LCSĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else if (A[i - 1] == B[j - 1])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = 1 + dp[i - 1][j - 1];Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If they are not equal thenĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // take the maxĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = Math.max(dp[i - 1][j],Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j - 1]);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Return difference of lengthĀ Ā Ā Ā // of A and lcs of A and BĀ Ā Ā Ā return n - dp[n][m];}Ā
// Driver CodeĀ
var N = 5;var M = 6;Ā
// Given sequence A and Bvar A = [1, 2, 3, 4, 5 ];var B = [2, 5, 6, 4, 9, 12 ];Ā
// Function calldocument.write( transformSubsequence(N, M, A, B));Ā
Ā
Ā
</script> |
3
Time Complexity: O(M*M), where N and M are the lengths of array A[] and B[] respectively.
Auxiliary Space: O(M*N)
Efficient approach : Space optimization
In previous approach the dp[i][j] is depend upon the current and previous row of 2D matrix. So to optimize space we use a 1D vectors dp to store previous value Ā and use prev to store the previous diagonal element and get the current computation.Ā
Implementation Steps:
- Define a vector dp of size m+1 and initialize its first element to 0.
- For each element j in B, iterate in reverse order from n to 1 and update dp[i] as follows:
a. If A[i-1] == B[j-1], set dp[i] to the previous value of dp[i-1] (diagonal element).
b. If A[i-1] != B[j-1], set dp[i] to the maximum value between dp[i] and dp[i-1]+1 (value on the left). - Finally, return n ā dp[m].
Implementation:
C++
// C++ program for above approachĀ
#include <bits/stdc++.h>using namespace std;Ā
// Function that finds the minimum number// of the element must be added to make A// as a subsequence in Bint transformSubsequence(int n, int m,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vector<int> A,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vector<int> B){Ā
Ā Ā Ā Ā // Base CaseĀ Ā Ā Ā if (B.size() == 0)Ā Ā Ā Ā Ā Ā Ā Ā return n;Ā
Ā Ā Ā Ā // dp[j] indicates the length ofĀ Ā Ā Ā // LCS of A and B of length jĀ Ā Ā Ā vector<int> dp(m + 1, 0);Ā
Ā Ā Ā Ā for(int i = 1; i < n + 1; i++)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int prev = dp[0];Ā Ā Ā Ā Ā Ā Ā Ā for(int j = 1; j < m + 1; j++)Ā Ā Ā Ā Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the element present atĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // ith and jth index of A and BĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // are equal then include in LCSĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int curr = dp[j];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (A[i - 1] == B[j - 1])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[j] = 1 + prev;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If they are not equal thenĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // take the maxĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[j] = max(dp[j], dp[j - 1]);Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā prev = curr;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Return difference of lengthĀ Ā Ā Ā // of A and lcs of A and BĀ Ā Ā Ā return n - dp[m];}Ā
// Driver Codeint main(){Ā Ā Ā Ā int N = 5;Ā Ā Ā Ā int M = 6;Ā
Ā Ā Ā Ā // Given sequence A and BĀ Ā Ā Ā vector<int> A = { 1, 2, 3, 4, 5 };Ā Ā Ā Ā vector<int> B = { 2, 5, 6, 4, 9, 12 };Ā
Ā Ā Ā Ā // Function callĀ Ā Ā Ā cout << transformSubsequence(N, M, A, B);Ā
Ā Ā Ā Ā return 0;}// this code is contributed by bhardwajji |
Java
import java.util.*;Ā
public class MinimumAdditions {Ā
Ā Ā Ā Ā // Function that finds the minimum numberĀ Ā Ā Ā // of the element must be added to make AĀ Ā Ā Ā // as a subsequence in BĀ Ā Ā Ā public static int transformSubsequence(int n, int m,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā List<Integer> A,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā List<Integer> B)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Base CaseĀ Ā Ā Ā Ā Ā Ā Ā if (B.size() == 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return n;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // dp[j] indicates the length ofĀ Ā Ā Ā Ā Ā Ā Ā // LCS of A and B of length jĀ Ā Ā Ā Ā Ā Ā Ā int[] dp = new int[m + 1];Ā
Ā Ā Ā Ā Ā Ā Ā Ā for(int i = 1; i < n + 1; i++)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int prev = dp[0];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for(int j = 1; j < m + 1; j++)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the element present atĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // ith and jth index of A and BĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // are equal then include in LCSĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int curr = dp[j];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (A.get(i - 1).equals(B.get(j - 1)))Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[j] = 1 + prev;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If they are not equal thenĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // take the maxĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[j] = Math.max(dp[j], dp[j - 1]);Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā prev = curr;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return difference of lengthĀ Ā Ā Ā Ā Ā Ā Ā // of A and lcs of A and BĀ Ā Ā Ā Ā Ā Ā Ā return n - dp[m];Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driver CodeĀ Ā Ā Ā public static void main(String[] args) {Ā Ā Ā Ā Ā Ā Ā Ā int N = 5;Ā Ā Ā Ā Ā Ā Ā Ā int M = 6;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Given sequence A and BĀ Ā Ā Ā Ā Ā Ā Ā List<Integer> A = Arrays.asList(1, 2, 3, 4, 5);Ā Ā Ā Ā Ā Ā Ā Ā List<Integer> B = Arrays.asList(2, 5, 6, 4, 9, 12);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Function callĀ Ā Ā Ā Ā Ā Ā Ā System.out.println(transformSubsequence(N, M, A, B));Ā Ā Ā Ā }} |
Python3
# Python program for above approachĀ
# Function that finds the minimum number# of the element must be added to make A# as a subsequence in Bdef transformSubsequence(n, m, A, B):Ā
Ā Ā Ā Ā # Base CaseĀ Ā Ā Ā if len(B) == 0:Ā Ā Ā Ā Ā Ā Ā Ā return nĀ
Ā Ā Ā Ā # dp[j] indicates the length ofĀ Ā Ā Ā # LCS of A and B of length jĀ Ā Ā Ā dp = [0] * (m + 1)Ā
Ā Ā Ā Ā for i in range(1, n + 1):Ā Ā Ā Ā Ā Ā Ā Ā prev = dp[0]Ā Ā Ā Ā Ā Ā Ā Ā for j in range(1, m + 1):Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If the element present atĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # ith and jth index of A and BĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # are equal then include in LCSĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = dp[j]Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if A[i - 1] == B[j - 1]:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[j] = 1 + prevĀ
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If they are not equal thenĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # take the maxĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[j] = max(dp[j], dp[j - 1])Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā prev = currĀ
Ā Ā Ā Ā # Return difference of lengthĀ Ā Ā Ā # of A and lcs of A and BĀ Ā Ā Ā return n - dp[m]Ā
# Driver Codeif __name__ == '__main__':Ā Ā Ā Ā N = 5Ā Ā Ā Ā M = 6Ā
Ā Ā Ā Ā # Given sequence A and BĀ Ā Ā Ā A = [1, 2, 3, 4, 5]Ā Ā Ā Ā B = [2, 5, 6, 4, 9, 12]Ā
Ā Ā Ā Ā # Function callĀ Ā Ā Ā print(transformSubsequence(N, M, A, B)) |
C#
using System;using System.Collections.Generic;using System.Linq;Ā
class Program{Ā Ā Ā Ā static int TransformSubsequence(int n, int m, List<int> A, List<int> B)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // Base CaseĀ Ā Ā Ā Ā Ā Ā Ā if (B.Count == 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return n;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // dp[j] indicates the length ofĀ Ā Ā Ā Ā Ā Ā Ā // LCS of A and B of length jĀ Ā Ā Ā Ā Ā Ā Ā var dp = new int[m + 1];Ā
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i < n + 1; i++)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int prev = dp[0];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 1; j < m + 1; j++)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the element present atĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // ith and jth index of A and BĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // are equal then include in LCSĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int curr = dp[j];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (A[i - 1] == B[j - 1])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[j] = 1 + prev;Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If they are not equal thenĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // take the maxĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[j] = Math.Max(dp[j], dp[j - 1]);Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā prev = curr;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return difference of lengthĀ Ā Ā Ā Ā Ā Ā Ā // of A and lcs of A and BĀ Ā Ā Ā Ā Ā Ā Ā return n - dp[m];Ā Ā Ā Ā }Ā
Ā Ā Ā Ā static void Main(string[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int N = 5;Ā Ā Ā Ā Ā Ā Ā Ā int M = 6;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Given sequence A and BĀ Ā Ā Ā Ā Ā Ā Ā var A = new List<int> { 1, 2, 3, 4, 5 };Ā Ā Ā Ā Ā Ā Ā Ā var B = new List<int> { 2, 5, 6, 4, 9, 12 };Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Function callĀ Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(TransformSubsequence(N, M, A, B));Ā Ā Ā Ā }} |
Javascript
// Define a function that finds the minimum number// of the element must be added to make A as a subsequence in Bfunction transformSubsequence(n, m, A, B) {Ā Ā Ā Ā // Base Case: if B is an empty list, then all elements of A Ā Ā Ā Ā // need to be added to B to make A a subsequence of Bif (B.length === 0)Ā Ā Ā Ā return n;Ā
// Define a dynamic programming array dp of length m+1 // where dp[j] indicates the length of the longest common subsequence (LCS) // of A and B of length jlet dp = new Array(m + 1).fill(0);Ā
// Loop through the elements of Afor(let i = 1; i < n + 1; i++) {Ā Ā Ā Ā // Define a variable prev to keep track of the value of dp[j-1] Ā Ā Ā Ā // in the previous iterationĀ Ā Ā Ā let prev = dp[0];Ā Ā Ā Ā // Loop through the elements of BĀ Ā Ā Ā for(let j = 1; j < m + 1; j++) {Ā Ā Ā Ā Ā Ā Ā Ā // Define a variable curr to keep track of the value of dp[j] Ā Ā Ā Ā Ā Ā Ā Ā // in the previous iterationĀ Ā Ā Ā Ā Ā Ā Ā let curr = dp[j];Ā Ā Ā Ā Ā Ā Ā Ā // If the ith element of A is equal to the jth element of B, Ā Ā Ā Ā Ā Ā Ā Ā // include this element in the LCSĀ Ā Ā Ā Ā Ā Ā Ā if (A[i - 1] === B[j - 1])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[j] = 1 + prev;Ā Ā Ā Ā Ā Ā Ā Ā // If the ith element of A is not equal to the jth element of B, Ā Ā Ā Ā Ā Ā Ā Ā // then take the maximum of dp[j] and dp[j-1] to find the Ā Ā Ā Ā Ā Ā Ā Ā // longest common subsequence so farĀ Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[j] = Math.max(dp[j], dp[j - 1]);Ā Ā Ā Ā Ā Ā Ā Ā // Update prev with the value of curr for the next iterationĀ Ā Ā Ā Ā Ā Ā Ā prev = curr;Ā Ā Ā Ā }}Ā
// Return the difference of the length of A and the LCS of A and B, which is the minimum number of elements that must be added to B to make A a subsequence of Breturn n - dp[m];}Ā
// Test the function with the given inputlet N = 5;let M = 6;Ā
let A = [1, 2, 3, 4, 5];let B = [2, 5, 6, 4, 9, 12];Ā
console.log(transformSubsequence(N, M, A, B)); |
Output
3
Time Complexity: O(M*M), where N and M are the lengths of array A[] and B[] respectively.
Auxiliary Space: O(M)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


