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
#include <bits/stdc++.h>
using namespace std;
int transformSubsequence(int n, int m,
vector<int> A,
vector<int> B)
{
if (B.size() == 0)
return n;
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 (i == 0 or j == 0)
dp[i][j] = 0;
else if (A[i - 1] == B[j - 1])
dp[i][j] = 1 + dp[i - 1][j - 1];
else
dp[i][j] = max(dp[i - 1][j],
dp[i][j - 1]);
}
}
return n - dp[n][m];
}
int main()
{
int N = 5;
int M = 6;
vector<int> A = { 1, 2, 3, 4, 5 };
vector<int> B = { 2, 5, 6, 4, 9, 12 };
cout << transformSubsequence(N, M, A, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int transformSubsequence(int n, int m,
int []A, int []B)
{
if (B.length == 0)
return n;
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 (i == 0 || j == 0)
dp[i][j] = 0;
else if (A[i - 1] == B[j - 1])
dp[i][j] = 1 + dp[i - 1][j - 1];
else
dp[i][j] = Math.max(dp[i - 1][j],
dp[i][j - 1]);
}
}
return n - dp[n][m];
}
public static void main(String[] args)
{
int N = 5;
int M = 6;
int []A = {1, 2, 3, 4, 5};
int []B = {2, 5, 6, 4, 9, 12};
System.out.print(transformSubsequence(N, M, A, B));
}
}
|
Python3
def transformSubsequence(n, m, A, B):
if B is None or len(B) == 0:
return n
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 i == 0 or j == 0:
dp[i][j] = 0
elif A[i-1] == B[j-1]:
dp[i][j] = 1 + dp[i-1][j-1]
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
return n - dp[n][m]
if __name__ == "__main__":
N = 5
M = 6
A = [1, 2, 3, 4, 5]
B = [2, 5, 6, 4, 9, 12]
print(transformSubsequence(N, M, A, B))
|
C#
using System;
class GFG{
static int transformSubsequence(int n, int m,
int []A, int []B)
{
if (B.Length == 0)
return n;
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 (i == 0 || j == 0)
dp[i, j] = 0;
else if (A[i - 1] == B[j - 1])
dp[i, j] = 1 + dp[i - 1, j - 1];
else
dp[i, j] = Math.Max(dp[i - 1, j],
dp[i, j - 1]);
}
}
return n - dp[n, m];
}
public static void Main(String[] args)
{
int N = 5;
int M = 6;
int []A = {1, 2, 3, 4, 5};
int []B = {2, 5, 6, 4, 9, 12};
Console.Write(transformSubsequence(N, M,
A, B));
}
}
|
Javascript
<script>
function transformSubsequence(n, m, A, B)
{
if (B.length == 0)
return n;
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 (i == 0 || j == 0)
dp[i][j] = 0;
else if (A[i - 1] == B[j - 1])
dp[i][j] = 1 + dp[i - 1][j - 1];
else
dp[i][j] = Math.max(dp[i - 1][j],
dp[i][j - 1]);
}
}
return n - dp[n][m];
}
var N = 5;
var M = 6;
var A = [1, 2, 3, 4, 5 ];
var B = [2, 5, 6, 4, 9, 12 ];
document.write( transformSubsequence(N, M, A, B));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int transformSubsequence(int n, int m,
vector<int> A,
vector<int> B)
{
if (B.size() == 0)
return n;
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++)
{
int curr = dp[j];
if (A[i - 1] == B[j - 1])
dp[j] = 1 + prev;
else
dp[j] = max(dp[j], dp[j - 1]);
prev = curr;
}
}
return n - dp[m];
}
int main()
{
int N = 5;
int M = 6;
vector<int> A = { 1, 2, 3, 4, 5 };
vector<int> B = { 2, 5, 6, 4, 9, 12 };
cout << transformSubsequence(N, M, A, B);
return 0;
}
|
Java
import java.util.*;
public class MinimumAdditions {
public static int transformSubsequence(int n, int m,
List<Integer> A,
List<Integer> B)
{
if (B.size() == 0)
return n;
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++)
{
int curr = dp[j];
if (A.get(i - 1).equals(B.get(j - 1)))
dp[j] = 1 + prev;
else
dp[j] = Math.max(dp[j], dp[j - 1]);
prev = curr;
}
}
return n - dp[m];
}
public static void main(String[] args) {
int N = 5;
int M = 6;
List<Integer> A = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> B = Arrays.asList(2, 5, 6, 4, 9, 12);
System.out.println(transformSubsequence(N, M, A, B));
}
}
|
Python3
def transformSubsequence(n, m, A, B):
if len(B) == 0:
return n
dp = [0] * (m + 1)
for i in range(1, n + 1):
prev = dp[0]
for j in range(1, m + 1):
curr = dp[j]
if A[i - 1] == B[j - 1]:
dp[j] = 1 + prev
else:
dp[j] = max(dp[j], dp[j - 1])
prev = curr
return n - dp[m]
if __name__ == '__main__':
N = 5
M = 6
A = [1, 2, 3, 4, 5]
B = [2, 5, 6, 4, 9, 12]
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)
{
if (B.Count == 0)
return n;
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++)
{
int curr = dp[j];
if (A[i - 1] == B[j - 1])
dp[j] = 1 + prev;
else
dp[j] = Math.Max(dp[j], dp[j - 1]);
prev = curr;
}
}
return n - dp[m];
}
static void Main(string[] args)
{
int N = 5;
int M = 6;
var A = new List<int> { 1, 2, 3, 4, 5 };
var B = new List<int> { 2, 5, 6, 4, 9, 12 };
Console.WriteLine(TransformSubsequence(N, M, A, B));
}
}
|
Javascript
function transformSubsequence(n, m, A, B) {
if (B.length === 0)
return n;
let dp = new Array(m + 1).fill(0);
for(let i = 1; i < n + 1; i++) {
let prev = dp[0];
for(let j = 1; j < m + 1; j++) {
let curr = dp[j];
if (A[i - 1] === B[j - 1])
dp[j] = 1 + prev;
else
dp[j] = Math.max(dp[j], dp[j - 1]);
prev = curr;
}
}
return n - dp[m];
}
let 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)