Minimize operations to delete all elements of permutation A by removing a subsequence having order as array B
Last Updated :
23 Jan, 2023
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: 3
Explanation:
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 2nd 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++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int A[], int B[], int N)
{
int cnt = 0;
int i = 0;
while (i < N) {
int j = 0;
while (j < N && i < N) {
if (B[i] == A[j]) {
i++;
}
j++;
}
cnt++;
}
return cnt;
}
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
import java.util.*;
class GFG{
static int minOperations( int A[], int B[], int N)
{
int cnt = 0 ;
int i = 0 ;
while (i < N) {
int j = 0 ;
while (j < N && i < N) {
if (B[i] == A[j]) {
i++;
}
j++;
}
cnt++;
}
return cnt;
}
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));
}
}
|
Python3
def minOperations(A, B, N):
cnt = 0
i = 0
while (i < N):
j = 0
while (j < N and i < N):
if (B[i] = = A[j]):
i + = 1
j + = 1
cnt + = 1
return cnt
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))
|
C#
using System;
class GFG {
static int minOperations( int [] A, int [] B, int N)
{
int cnt = 0;
int i = 0;
while (i < N) {
int j = 0;
while (j < N && i < N) {
if (B[i] == A[j]) {
i++;
}
j++;
}
cnt++;
}
return cnt;
}
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));
}
}
|
Javascript
<script>
function minOperations(A, B, N) {
let cnt = 0;
let i = 0;
while (i < N) {
let j = 0;
while (j < N && i < N) {
if (B[i] == A[j]) {
i++;
}
j++;
}
cnt++;
}
return cnt;
}
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));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...