Minimize elements to be added to a given array such that it contains another given array as its subsequence
Last Updated :
15 Jul, 2025
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 B
int 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 Code
int 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 approach
import java.util.*;
class GFG{
// Function that finds the minimum number
// of the element must be added to make A
// as a subsequence in B
static 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 Code
public 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 B
def 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 Code
if __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 approach
using System;
class GFG{
// Function that finds the minimum number
// of the element must be added to make A
// as a subsequence in B
static 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 Code
public 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 B
function 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 B
var A = [1, 2, 3, 4, 5 ];
var B = [2, 5, 6, 4, 9, 12 ];
// Function call
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++
// 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 B
int 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 Code
int 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 B
def 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 Code
if __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 B
function 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 B
if (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 j
let dp = new Array(m + 1).fill(0);
// Loop through the elements of A
for(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 B
return n - dp[m];
}
// Test the function with the given input
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)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem