Minimum insertions.

Hard
0/120
Average time to solve is 45m
profile
Contributed by
3 upvotes
Asked in companies
ApplePureSoftware Ltd

Problem statement

Alice was recently studying subsequence. She discovered that it is possible to make an array of ‘N’ integers say ‘arr1’, a subsequence of another array of ‘M’ integers, say ‘arr2’, by inserting 0 or more integers into the array ‘arr2’. As she is busy with her studies, she asked you to find the minimum number of integers that need to be inserted into ‘arr2’ to make ‘arr1’ a subsequence of ‘arr2’.

Note :

1) ‘arr1’ consists of distinct positive integers, and ‘arr2’ can contain duplicates.
2) You are allowed to insert integers at any position in ‘arr2’. For example, if ‘arr2’  = {1, 2, 3}, you can insert 4 at 2nd position to make it {1, 4, 2, 3}.
Detailed explanation ( Input/output format, Notes, Images )

Input Format :

The first line of input contains an integer 'T' representing the number of test cases.

The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, where ‘N’ and ‘M’ denote the total number of elements in ‘arr1’ and ‘arr2’, respectively.

The second line of each test case contains ‘N’ space-separated integers, denoting the elements of ‘arr1’.

The second line of each test case contains ‘M’ space-separated integers, denoting the elements of ‘arr2’.

Output Format :

For each test case, print the minimum number of integers that need to be inserted into ‘arr2’ to make ‘arr1’ a subsequence of ‘arr2’.

Constraints :

1 <= T <= 5
1 <= N, M <= 1000
1 <= arr1[i], arr2[i] <= 10 ^ 9

Where ‘T’ is the number of test cases,  ‘N’, ‘M’ denotes the number of elements in ‘arr1’ and ‘arr2’, respectively, and ‘arr1[i]’, ‘arr2[i]’ denotes the i-th element in ‘arr1’ and ‘arr2’, respectively.

Note :

 You do not need to print anything, it has already been taken care of. Just implement the given function.

Sample Input 1 :

2
3 5
1 2 3
3 2 1 4 7
4 6
2 1 7 3
7 1 7 2 3 1      

Sample Output 1 :

2
1

Explanation of Sample Input 1 :

Test Case 1 :  
‘arr1’ = {1, 2, 3} and ‘arr2’ = {3, 2, 1, 4, 7}.
We can insert 2 at the 4th position in ‘arr2’. After insertion, ‘arr2’ = {3, 2, 1, 2, 4, 7}.
We can insert 3 at the 5th position in ‘arr2’. After insertion, ‘arr2’ = {3, 2, 1, 2, 3, 4, 7}.
‘arr1’ is now a subsequence of ‘arr2’. Therefore, the number of insertions needed is 2.

Test Case 2 : 
‘arr1’ = {2, 1, 7, 3} and ‘arr2’ = {7, 1, 7, 2, 3, 1}.
We can insert 2 at the 2nh position in ‘arr2’. After insertion, ‘arr2’ = {7, 2, 1, 7, 2, 3, 1}.
‘arr1’ is now a subsequence of ‘arr2’. Therefore, the number of insertions needed is 1.

Sample Input 2 :

1
3 7
4 1 2
8 4 9 2 1 2 9

Sample Output 2 :

0

Explanation of Sample Input 2 :

Test Case 1 :  
‘arr1’ = {4, 1, 2} and ‘arr2’ = {8, 4, 9, 2, 1, 2, 9}.
‘arr1’ is already a subsequence of ‘arr2’. Therefore, we do not insert any integer in ‘arr2’.
Hint

Try to insert each integer of ‘arr1’ at every possible position in ‘arr2’.

Approaches (4)
Brute Force

The idea here is to insert every integer of ‘arr1’ at every possible position in the ‘arr2’. After inserting all the elements of ‘arr1’ in ‘arr2’, ‘arr1’ will become a subsequence of ‘arr2’. Out of all such series of insertions, we will take the one with the minimum number of insertions.

 

Algorithm:

 

  • Declare an integer, say ‘minInsertions’, to store the minimum number of integers that need to be inserted into ‘arr2’ to make ‘arr1’ a subsequence of  ‘arr2’.
  • Call the ‘allInsertions’ function with ‘arr1‘, ‘arr2’, 0, 0, ‘N’, and ‘M’ as arguments, and store its returned value into ‘minInsertions’.
  • Return ‘minInsertions’.

 

Description of ‘allInsertions’ function

 

This function is used to insert all the elements of  ‘arr1’ from ‘low1’ to ‘N’ - 1  inclusive into every possible position from ‘low2’ to ‘M’ - 1 inclusive, in ‘arr2’.

This function will take six parameters :

  • arr1: An array of ‘N’ integers given in the problem.
  • arr2: An array of ‘M’ integers given in the problem.
  • low1: An integer denoting the starting index of ‘arr1’
  • low2: An integer denoting the starting index of ‘arr2’
  • N: An integer denoting the number of elements in ‘arr1’
  • M: An integer denoting the number of elements in ‘arr2’

int allInsertions(arr1, arr2, low1, low2, N, M):

  • If ‘low1’ equals ‘N’.
    • Return 0, as we don’t have any element in ‘arr1’ to insert in ‘arr2’.
  • If ‘low2’ equals ‘M’.
    • Return (N - low1), as this number of elements, needs to be inserted in ‘arr2’ to make ‘arr1’ a subsequence.
  • Declare a variable to keep track of the optimal insertions, say ‘answer’.
  • If ‘arr1[low1]’ is the same as ‘arr2[low2]’:
    • This means that ‘arr1[low1]’ is already present in ‘arr2’
    • Recursively call the ‘allInsertions’ function with ‘arr1‘, ‘arr2’, low1 + 1, low2 + 1, ‘N’, and ‘M’ as arguments, and store its returned value into ‘answer’.
  • Else:
    • Otherwise, we will insert ‘arr1[low1]’ in ‘arr2’ at ‘low2’.
    • Recursively call the ‘allInsertions’ function with ‘arr1‘, ‘arr2’, low1 + 1, low2, ‘N’, and ‘M’ as arguments, and store its returned value into ‘answer’.
    • Increment ‘answer’ as we have inserted an element in ‘arr2’, i.e., do ‘answer’ = answer + 1.
  • As we need to make ‘arr1’ a subsequence of ‘arr2’ we can skip the current position of ‘arr2’ and try to insert ‘arr1[low1]’ in some higher positions.
  • Recursively call the ‘allInsertions’ function with ‘arr1‘, ‘arr2’, low1, low2 + 1, ‘N’, and ‘M’ as arguments, and store its returned value into a variable, say ‘currAnswer’.
  • Update ‘answer’ as the minimum of ‘answer’ and ‘currAnswer’.
  • Return ‘answer’.
Time Complexity

O(2 ^ (N + M)), where ‘N’ and ‘M’ denote the number elements in ‘arr1’ and ‘arr2’, respectively.

 

In the worst case, we have to insert all the elements of ‘arr1’ into the ‘arr2’ array. After insertion, the size of ‘arr2’ will be ‘N’ + ‘M’. For any i-th position in ‘arr2’, we have two choices, either we will insert an element at the i-th position, or we will not insert.

Therefore, we have a total of 2 ^ (N + M) different ways to insert elements of ‘arr1’ into the ‘arr2’ array, and for each insertion, we take O(1) time. Hence, the total time complexity will be O(2 ^ (N + M)).

Space Complexity

O(N + M), where ‘N’ and ‘M’ denote the number elements in ‘arr1’ and ‘arr2’, respectively.

 

Our ‘allInsertions’ function requires O(N + M) space in the form of recursive stack space.

Therefore, the total space complexity will be O(N + M).

Code Solution
(100% EXP penalty)
Minimum insertions.
Full screen
Console