# Minimize operations to delete all elements of permutation A by removing a subsequence having order as array B

Given two permutation arrays** A[]** and **B[]** of the first **N ****Natural Numbers**, the task is to find the minimum number of operations required to remove all array elements **A[]** such that in each operation remove the subsequence of array elements **A[]** whose order is the same as in the array **B[]**.

**Example:**

Input:A[] = { 4, 2, 1, 3 }, B[] = { 1, 3, 2, 4 }Output:3Explanation:

The given example can be solved by following the given steps:

- During the 1st operation, integers at index 2 and 3 in the array A[] can be deleted. Hence, the array A[] = {4, 2}.
- During the 2st operation, integer at index 1 in the array A[] can be deleted. Hence, the array A[] = {4}.
- During the 3rd operation, integer at index 0 in the array A[] can be deleted. Hence, the array A[] = {}.
The order in which the elements are deleted is {1, 3, 2, 4} which is equal to B. Hence a minimum of 3 operations is required.

Input:A[] = {2, 4, 6, 1, 5, 3}, B[] = {6, 5, 4, 2, 3, 1}Output:4

**Approach:** The given problem can be solved using the steps discussed below:

- Create two variables
**i**and**j**, where**i**keeps track of the index of the current element of**B**that is to be deleted next and**j**keeps track of the current element of**A**. Initially, both**i=0**and**j=0**. - Traverse the permutation array
**A[]**using**j**for all values of**j**in range**[0, N-1]**. If**A[j] = B[i]**, increment the value of**i**by 1 and continue traversing the array**A[]**. - After the array
**A[]**has been traversed completely, increment the value of**cnt**variable which maintains the count of required operations. - Repeat steps
**2**and**3**till**i<N**. - After completing the above steps, the value stored in
**cnt**is the required answer.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the minimum number of` `// operations to delete all elements of` `// permutation A in order described by B` `int` `minOperations(` `int` `A[], ` `int` `B[], ` `int` `N)` `{` ` ` `// Stores the count of operations` ` ` `int` `cnt = 0;` ` ` `// Stores the index of current integer` ` ` `// in B to be deleted` ` ` `int` `i = 0;` ` ` `// Loop to iterate over all values of B` ` ` `while` `(i < N) {` ` ` `// Stores the current index in A` ` ` `int` `j = 0;` ` ` `// Iterate over all values A` ` ` `while` `(j < N && i < N) {` ` ` `// If current integer of B and A` ` ` `// equal, increment the index of` ` ` `// the current integer of B` ` ` `if` `(B[i] == A[j]) {` ` ` `i++;` ` ` `}` ` ` `j++;` ` ` `}` ` ` `// As the permutation A has been` ` ` `// traversed completelly, increment` ` ` `// the count of operations by 1` ` ` `cnt++;` ` ` `}` ` ` `// Return Answer` ` ` `return` `cnt;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `A[] = { 2, 4, 6, 1, 5, 3 };` ` ` `int` `B[] = { 6, 5, 4, 2, 3, 1 };` ` ` `int` `N = ` `sizeof` `(A) / ` `sizeof` `(A[0]);` ` ` `cout << minOperations(A, B, N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to find the minimum number of` `// operations to delete all elements of` `// permutation A in order described by B` `static` `int` `minOperations(` `int` `A[], ` `int` `B[], ` `int` `N)` `{` ` ` ` ` `// Stores the count of operations` ` ` `int` `cnt = ` `0` `;` ` ` `// Stores the index of current integer` ` ` `// in B to be deleted` ` ` `int` `i = ` `0` `;` ` ` `// Loop to iterate over all values of B` ` ` `while` `(i < N) {` ` ` `// Stores the current index in A` ` ` `int` `j = ` `0` `;` ` ` `// Iterate over all values A` ` ` `while` `(j < N && i < N) {` ` ` `// If current integer of B and A` ` ` `// equal, increment the index of` ` ` `// the current integer of B` ` ` `if` `(B[i] == A[j]) {` ` ` `i++;` ` ` `}` ` ` `j++;` ` ` `}` ` ` `// As the permutation A has been` ` ` `// traversed completely, increment` ` ` `// the count of operations by 1` ` ` `cnt++;` ` ` `}` ` ` `// Return Answer` ` ` `return` `cnt;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `A[] = { ` `2` `, ` `4` `, ` `6` `, ` `1` `, ` `5` `, ` `3` `};` ` ` `int` `B[] = { ` `6` `, ` `5` `, ` `4` `, ` `2` `, ` `3` `, ` `1` `};` ` ` `int` `N = A.length;` ` ` `System.out.print(minOperations(A, B, N));` `}` `}` `// This code is contributed by 29AjayKumar` |

## Python3

`# Python 3 program for the above approach` `# Function to find the minimum number of` `# operations to delete all elements of` `# permutation A in order described by B` `def` `minOperations(A, B, N):` ` ` ` ` `# Stores the count of operations` ` ` `cnt ` `=` `0` ` ` `# Stores the index of current integer` ` ` `# in B to be deleted` ` ` `i ` `=` `0` ` ` `# Loop to iterate over all values of B` ` ` `while` `(i < N):` ` ` ` ` `# Stores the current index in A` ` ` `j ` `=` `0` ` ` `# Iterate over all values A` ` ` `while` `(j < N ` `and` `i < N):` ` ` `# If current integer of B and A` ` ` `# equal, increment the index of` ` ` `# the current integer of B` ` ` `if` `(B[i] ` `=` `=` `A[j]):` ` ` `i ` `+` `=` `1` ` ` `j ` `+` `=` `1` ` ` `# As the permutation A has been` ` ` `# traversed completely, increment` ` ` `# the count of operations by 1` ` ` `cnt ` `+` `=` `1` ` ` `# Return Answer` ` ` `return` `cnt` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `A ` `=` `[` `2` `, ` `4` `, ` `6` `, ` `1` `, ` `5` `, ` `3` `]` ` ` `B ` `=` `[` `6` `, ` `5` `, ` `4` `, ` `2` `, ` `3` `, ` `1` `]` ` ` `N ` `=` `len` `(A)` ` ` `print` `(minOperations(A, B, N))` ` ` `# This code is contributed by SURENDRA_GANGWAR.` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG {` ` ` `// Function to find the minimum number of` ` ` `// operations to delete all elements of` ` ` `// permutation A in order described by B` ` ` `static` `int` `minOperations(` `int` `[] A, ` `int` `[] B, ` `int` `N)` ` ` `{` ` ` `// Stores the count of operations` ` ` `int` `cnt = 0;` ` ` `// Stores the index of current integer` ` ` `// in B to be deleted` ` ` `int` `i = 0;` ` ` `// Loop to iterate over all values of B` ` ` `while` `(i < N) {` ` ` `// Stores the current index in A` ` ` `int` `j = 0;` ` ` `// Iterate over all values A` ` ` `while` `(j < N && i < N) {` ` ` `// If current integer of B and A` ` ` `// equal, increment the index of` ` ` `// the current integer of B` ` ` `if` `(B[i] == A[j]) {` ` ` `i++;` ` ` `}` ` ` `j++;` ` ` `}` ` ` `// As the permutation A has been` ` ` `// traversed completely, increment` ` ` `// the count of operations by 1` ` ` `cnt++;` ` ` `}` ` ` `// Return Answer` ` ` `return` `cnt;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main(` `string` `[] args)` ` ` `{` ` ` `int` `[] A = { 2, 4, 6, 1, 5, 3 };` ` ` `int` `[] B = { 6, 5, 4, 2, 3, 1 };` ` ` `int` `N = A.Length;` ` ` `Console.WriteLine(minOperations(A, B, N));` ` ` `}` `}` `// This code is contributed by ukasp.` |

## Javascript

`<script>` ` ` `// JavaScript Program to implement` ` ` `// the above approach` ` ` `// Function to find the minimum number of` ` ` `// operations to delete all elements of` ` ` `// permutation A in order described by B` ` ` `function` `minOperations(A, B, N) {` ` ` `// Stores the count of operations` ` ` `let cnt = 0;` ` ` `// Stores the index of current integer` ` ` `// in B to be deleted` ` ` `let i = 0;` ` ` `// Loop to iterate over all values of B` ` ` `while` `(i < N) {` ` ` `// Stores the current index in A` ` ` `let j = 0;` ` ` `// Iterate over all values A` ` ` `while` `(j < N && i < N) {` ` ` `// If current integer of B and A` ` ` `// equal, increment the index of` ` ` `// the current integer of B` ` ` `if` `(B[i] == A[j]) {` ` ` `i++;` ` ` `}` ` ` `j++;` ` ` `}` ` ` `// As the permutation A has been` ` ` `// traversed completely, increment` ` ` `// the count of operations by 1` ` ` `cnt++;` ` ` `}` ` ` `// Return Answer` ` ` `return` `cnt;` ` ` `}` ` ` `// Driver Code` ` ` `let A = [2, 4, 6, 1, 5, 3];` ` ` `let B = [6, 5, 4, 2, 3, 1];` ` ` `let N = A.length;` ` ` `document.write(minOperations(A, B, N));` ` ` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output:**

4

**Time Complexity:** O(N^{2})**Auxiliary Space:** O(1)