Minimum Operations to Make a Subsequence - Problem

You're tasked with transforming an array to make a specific sequence appear as a subsequence. Given a target array of distinct integers and an arr array that may contain duplicates, you need to find the minimum number of insertion operations required.

In each operation, you can insert any integer at any position in arr. For example, if arr = [1,4,1,2], you can insert 3 to get [1,4,3,1,2].

Goal: Return the minimum operations needed to make target a subsequence of the modified arr.

Note: A subsequence maintains the relative order of elements but allows deletions. For example, [2,7,4] is a subsequence of [4,2,3,7,2,1,4], but [2,4,2] is not.

Input & Output

example_1.py โ€” Basic Example
$ Input: target = [5,1,3], arr = [9,4,2,3,4]
โ€บ Output: 2
๐Ÿ’ก Note: We need to insert 5 and 1 to make [5,1,3] a subsequence. Only 3 already exists in arr in the correct position.
example_2.py โ€” All Elements Present
$ Input: target = [6,4,8,1,3], arr = [4,7,6,2,3,4,6,1,8]
โ€บ Output: 3
๐Ÿ’ก Note: The longest subsequence we can form from existing elements is [6,4,8] or [4,1,3] (length 3). So we need to insert 5-3=2 elements... Wait, let me recalculate: we can form [6,1,3] or [4,8,3] giving us length 3, so we need 5-3=2 insertions.
example_3.py โ€” No Common Elements
$ Input: target = [5,1,3], arr = [9,4,2,7,6]
โ€บ Output: 3
๐Ÿ’ก Note: No elements from target exist in arr, so we need to insert all 3 elements from target.

Visualization

Tap to expand
๐Ÿณ Recipe Ingredient Optimization๐Ÿ“‹ Recipe Order1. Flour (6)2. Butter (4)3. Sugar (8)4. Eggs (1)๐Ÿฅซ Pantry ContentsButter, Flour, SugarSalt, Eggs, MilkPositions: [4,6,8,1]โ†’ Indices: [1,0,2,3]๐Ÿ“ˆ LIS AnalysisSequence: [1,0,2,3]LIS: [0,2,3] or [1,2,3]Length: 3Can use 3 ingredients๐Ÿ›’ ResultNeed to buy:4 - 3 = 1ingredient๐Ÿ” LIS Binary Search ProcessProcess sequence [1,0,2,3]:โ€ข dp = [] โ†’ add 1 โ†’ dp = [1]โ€ข 0 < 1 โ†’ replace 1 โ†’ dp = [0]โ€ข 2 > 0 โ†’ append โ†’ dp = [0,2]โ€ข 3 > 2 โ†’ append โ†’ dp = [0,2,3]Final LIS length: 3
Understanding the Visualization
1
Create ingredient map
Map each recipe ingredient to its required position
2
Check pantry
Go through pantry and note positions of recipe ingredients we have
3
Find best sequence
Find longest increasing sequence of positions (ingredients in correct order)
4
Calculate shopping list
Ingredients to buy = Total needed - Longest sequence we have
Key Takeaway
๐ŸŽฏ Key Insight: The minimum insertions needed equals the total target length minus the length of the longest subsequence we can already form in the correct order - efficiently found using the Longest Increasing Subsequence algorithm!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒร—m)

For each of n target elements, we try m arr elements, with recursive depth of n

n
2n
โš  Quadratic Growth
Space Complexity
O(nร—m)

Memoization table size plus recursion stack depth

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค target.length, arr.length โ‰ค 105
  • 1 โ‰ค target[i], arr[i] โ‰ค 109
  • target contains distinct integers
  • arr can contain duplicate integers
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
36.2K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen