Skip to main content

Minimum Operations to Make a Subsequence - Solution & Explanation

HardArrayHash TableBinary SearchGreedy24 min readAsked at: Amazon, Google
Practice this problem

Problem Statement

You are given an array target that consists of distinct integers and another integer array arr that can have duplicates.

In one operation, you can insert any integer at any position in arr. For example, if arr = [1,4,1,2], you can add 3 in the middle and make it [1,4,3,1,2]. Note that you can insert the integer at the very beginning or end of the array.

Return the minimum number of operations needed to make target a subsequence of arr.

A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order. For example, [2,7,4] is a subsequence of [4,2,3,7,2,1,4] (the underlined elements), while [2,4,2] is not.

 

Example 1:

Input: target = [5,1,3], arr = [9,4,2,3,4]
Output: 2
Explanation: You can add 5 and 1 in such a way that makes arr = [5,9,4,1,2,3,4], then target will be a subsequence of arr.

Example 2:

Input: target = [6,4,8,1,3,2], arr = [4,7,6,2,3,8,6,1]
Output: 3

 

Constraints:

  • 1 <= target.length, arr.length <= 105
  • 1 <= target[i], arr[i] <= 109
  • target contains no duplicates.

Approach Overview

Problem Overview: You are given two arrays: target (with distinct values) and arr. The task is to compute the minimum number of insert operations needed so that target becomes a subsequence of arr. Instead of simulating insertions directly, the key observation is that maximizing the part of target already appearing in order inside arr minimizes the required operations.

Approach 1: Dynamic Programming (O(n * m) time, O(n * m) space)

This approach treats the problem as a variation of the Longest Common Subsequence (LCS). Compute the LCS between target and arr using a DP table where dp[i][j] represents the LCS length for the first i elements of target and first j elements of arr. If elements match, extend the subsequence; otherwise take the maximum of skipping one element from either array. The minimum operations required equals target.length - LCS. While straightforward and useful for understanding subsequence alignment, the O(n * m) time complexity becomes too slow for large inputs.

Approach 2: Hash Map + Longest Increasing Subsequence (O(n log n) time, O(n) space)

The optimal solution converts the problem into a Longest Increasing Subsequence (LIS) problem. Since target contains unique values, create a hash map mapping each value to its index in target. Then iterate through arr and replace each element with its mapped index if it exists in the map. This produces a sequence of indices representing the order of elements relative to target.

The goal becomes finding the longest increasing subsequence of these indices. An increasing sequence means elements appear in the correct order relative to target. Compute LIS efficiently using binary search and a dynamic array that stores the smallest tail value for subsequences of each length. Each index either extends the sequence or replaces a larger tail via binary search.

If the LIS length is k, then k elements of target already appear in correct order inside arr. The remaining target.length - k elements must be inserted. This reduces the problem to a classic binary search optimization combined with hash table indexing and sequence processing over an array.

Recommended for interviews: The hash map + LIS approach is what most interviewers expect. It demonstrates the ability to transform a subsequence matching problem into an index-ordering problem and apply the O(n log n) LIS technique. The dynamic programming solution shows understanding of subsequences but does not scale well for the input constraints.

Approach 1: Using Hash Map and Longest Increasing Subsequence (LIS)

This approach uses a hash map (or dictionary) to map each element of the target to its index. We then convert the problem into finding the longest increasing subsequence in terms of the indices in the target array. By doing so, the number of insertions required corresponds to the difference between the target array length and the length of this subsequence.

The C code creates a position map (pos) to store indexes of target elements. It uses a list lis to track the indices of elements in arr present in target, forming the longest increasing subsequence. The bisect_left function is used to find the insertion point in the lis, similar to the binary search approach of finding the increasing subsequence. The result is calculated by subtracting the length of lis from the length of target.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

The solution has a time complexity of O(n log n), where n is the length of arr. The space complexity is O(n) due to the use of the lis list and the position map.

Try this approach in the editor →

Approach 2: Using Dynamic Programming Approach

This approach uses dynamic programming to directly compute the operations necessary to form target directly. It keeps track of the length of the longest subsequence that can be achieved up to each position in arr, using knowledge of target positioning.

The C solution uses a dynamic programming array dp to keep track of the maximum subsequence length that can be achieved at every point in arr. The algorithm iterates over arr, updating dp based on whether elements match between target and arr. The result is derived by subtracting the length of the longest valid subsequence from the length of target.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

The solution provides a time complexity of O(n * m) and a space complexity of O(m), where n is the size of arr and m is the size of target. It balances direct comparison with memory efficiency.

Try this approach in the editor →

Approach 3: Longest Increasing Subsequence + Binary Indexed Tree

According to the problem statement, the longer the common subsequence between target and arr, the fewer elements need to be added. Therefore, the minimum number of elements to be added equals the length of target minus the length of the longest common subsequence between target and arr.

However, the time complexity of finding the longest common subsequence is O(m times n), which cannot pass this problem. We need to change our approach.

We can use a hash table to record the index of each element in the target array, then iterate through the arr array. For each element in the arr array, if the hash table contains that element, we add the index of that element to an array. This gives us a new array nums, which represents the indices in the target array of elements from arr (excluding elements not in target). The length of the longest increasing subsequence of this array nums is the length of the longest common subsequence between target and arr.

Therefore, the problem is transformed into finding the length of the longest increasing subsequence of the nums array. Refer to 300. Longest Increasing Subsequence.

The time complexity is O(n times log m), and the space complexity is O(m). Here, m and n are the lengths of target and arr, respectively.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using Hash Map and Longest Increasing Subsequence (LIS)

The solution has a time complexity of O(n log n), where n is the length of arr. The space complexity is O(n) due to the use of the lis list and the position map.

Using Dynamic Programming Approach

The solution provides a time complexity of O(n * m) and a space complexity of O(m), where n is the size of arr and m is the size of target. It balances direct comparison with memory efficiency.

Longest Increasing Subsequence + Binary Indexed Tree

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Dynamic Programming (LCS)O(n * m)O(n * m)Good for learning subsequence matching or when constraints are small
Hash Map + Longest Increasing SubsequenceO(n log n)O(n)Optimal for large inputs; converts order matching into LIS using binary search

Video Solution

【LCS/LIS】花花酱 LeetCode 1713. Minimum Operations to Make a Subsequence - 刷题找工作 EP379Hua Hua1,657 views views

Watch 6 more video solutions →

Frequently Asked Questions

Is Minimum Operations to Make a Subsequence easy or hard?
LeetCode classifies this problem as Hard because it requires recognizing the transformation to a Longest Increasing Subsequence problem. Once that insight is clear, the implementation becomes a standard O(n log n) LIS solution using binary search.
Minimum Operations to Make a Subsequence Python or Java solution?
Python and Java solutions both follow the same strategy: build a map from target values to indices, convert arr into a filtered index sequence, and compute LIS using binary search. The logic remains identical across languages, with only syntax differences in map creation and binary search utilities.
How to solve Minimum Operations to Make a Subsequence in O(n log n)?
First map each value in target to its index using a hash table. Iterate through arr and build a list of indices corresponding to elements that appear in target. Then compute the Longest Increasing Subsequence of this index list using binary search. The number of insert operations required is target.length minus the LIS length.
What is the best approach for Minimum Operations to Make a Subsequence?
The optimal approach uses a hash map combined with the Longest Increasing Subsequence (LIS) algorithm. Map each value in target to its index, convert arr into a sequence of those indices, and compute LIS using binary search. The LIS length represents the longest ordered match, and the required operations equal target.length minus that length. This runs in O(n log n) time with O(n) space.
Is Minimum Operations to Make a Subsequence asked at Google/Amazon/Meta?
Problems involving LIS transformations, subsequences, and hash map indexing are common in interviews at companies like Google, Amazon, and Meta. This specific LeetCode problem tests the ability to reduce a sequence alignment problem into an LIS problem with binary search optimization.
What data structure is used in Minimum Operations to Make a Subsequence?
The main data structures are a hash map for mapping values to indices and a dynamic array used in the LIS algorithm. Binary search is applied on this array to maintain the smallest possible tail values for subsequences of each length.
What is the time complexity of Minimum Operations to Make a Subsequence?
The optimal solution runs in O(n log n) time using a hash map and binary search based LIS algorithm. Building the index map takes O(n), scanning arr takes O(m), and each LIS update costs O(log n). A brute-force dynamic programming approach using LCS would take O(n * m) time and is not practical for large inputs.

Ready to solve this problem?

Practice Minimum Operations to Make a Subsequence with our built-in code editor and test cases.

Practice on FleetCode