Count of array elements whose order of deletion precedes order of insertion
Last Updated :
17 Jul, 2021
Given an initial array, A[] and a final array B[] both of size N containing integers from the range [1, N], where A[] represent the order in which elements were inserted and B[] represents the order in which they were removed, the task is to find the number of elements in B[] lying at an index earlier than its respective position in A[].
Examples:
Input: A[ ] = {1, 2, 4, 3}, B[ ] = {2, 3, 4, 1}
Output: 3
Explanation:
The element 2 was inserted after 1, but removed before 1.
The element 3 was inserted after 4, but removed before 4.
The element 4 was inserted after 1, but removed before 1.
Hence, total 3 elements have moved ahead.
Input: A[ ] = {1, 2, 3, 4} B[ ] = {1, 2, 4, 3}
Output: 1
Explanation:
The element 4 was inserted after 3, but removed before 3.
Hence, only 1 element has moved ahead.
Naive Approach: The simplest way to solve this problem is to compare the position of every element in B[] with the position of every other element in A[] and check if it was inserted after an element in A[] but removed before in B[]. If yes, then increment the count. Finally, print the total count.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: A better approach to solve this problem is to maintain a queue and insert the elements of B[] into this queue and also insert all elements to an unordered set. An unordered set is used to efficiently check if a particular element is processed yet or not. Traverse A[] and pop elements from the queue until current element A[i] appears at top of the queue, also keep incrementing the count and marking the elements appearing at top of the queue as processed. Finally, the total count will give the number of elements that have moved ahead in B[].
Follow the steps below to solve the problem:
- Maintain a Queue and unordered set and store the values of array B[] in both.
- Now, iterate over array A[].
- If the current element is not present in the unordered set, it means it has already been removed.
- Initialize a variable count. If any ith element is not found in the queue, remove all the elements present in the queue before A[i] from the queue and unordered set keep increasing count.
- Remove A[i] from the queue and unordered set.
- Finally, print the total count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumCount( int A[], int B[], int n)
{
queue< int > q;
unordered_set< int > s;
for ( int i = 0; i < n; i++) {
s.insert(B[i]);
q.push(B[i]);
}
int count = 0;
for ( int i = 0; i < n; i++) {
if (s.find(A[i]) == s.end())
continue ;
while (!q.empty() && q.front() != A[i]) {
s.erase(q.front());
q.pop();
count++;
}
if (A[i] == q.front()) {
q.pop();
s.erase(A[i]);
}
if (q.empty())
break ;
}
cout << count << endl;
}
int main()
{
int N = 4;
int A[] = { 1, 2, 3, 4 };
int B[] = { 1, 2, 4, 3 };
maximumCount(A, B, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maximumCount( int A[], int B[], int n)
{
Queue<Integer> q = new LinkedList<>();
HashSet<Integer> s = new HashSet<>();
for ( int i = 0 ; i < n; i++)
{
s.add(B[i]);
q.add(B[i]);
}
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (!s.contains(A[i]))
continue ;
while (!q.isEmpty() && q.peek() != A[i])
{
s.remove(q.peek());
q.remove();
count++;
}
if (A[i] == q.peek())
{
q.remove();
s.remove(A[i]);
}
if (q.isEmpty())
break ;
}
System.out.print(count + "\n" );
}
public static void main(String[] args)
{
int N = 4 ;
int A[] = { 1 , 2 , 3 , 4 };
int B[] = { 1 , 2 , 4 , 3 };
maximumCount(A, B, N);
}
}
|
Python3
import queue
def maximumCount(A, B, n):
q = queue.Queue()
s = set ()
for i in range (n):
s.add(B[i])
q.put(B[i])
count = 0
for i in range (n):
if (A[i] not in s):
continue
while (q.qsize() > 0 and
q.queue[ 0 ] ! = A[i]):
s.remove(q.queue[ 0 ]);
q.get()
count + = 1
if (A[i] = = q.queue[ 0 ]):
q.get()
s.remove(A[i])
if (q.qsize() = = 0 ):
break
print (count)
N = 4
A = [ 1 , 2 , 3 , 4 ]
B = [ 1 , 2 , 4 , 3 ]
maximumCount(A, B, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void maximumCount( int []A, int []B, int n)
{
Queue< int > q = new Queue< int >();
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < n; i++)
{
s.Add(B[i]);
q.Enqueue(B[i]);
}
int count = 0;
for ( int i = 0; i < n; i++)
{
if (!s.Contains(A[i]))
continue ;
while (q.Count != 0 && q.Peek() != A[i])
{
s.Remove(q.Peek());
q.Dequeue();
count++;
}
if (A[i] == q.Peek())
{
q.Dequeue();
s.Remove(A[i]);
}
if (q.Count == 0)
break ;
}
Console.Write(count + "\n" );
}
public static void Main(String[] args)
{
int N = 4;
int []A = { 1, 2, 3, 4 };
int []B = { 1, 2, 4, 3 };
maximumCount(A, B, N);
}
}
|
Javascript
<script>
function maximumCount(A, B, n)
{
var q = [];
var s = new Set();
for ( var i = 0; i < n; i++) {
s.add(B[i]);
q.push(B[i]);
}
var count = 0;
for ( var i = 0; i < n; i++) {
if (s.has(A[i]))
{
while (q.length!=0 && q[0] != A[i]) {
s. delete (q[0]);
q.shift();
count++;
}
if (A[i] == q[0]) {
q.shift();
s. delete (A[i]);
}
if (q.length==0)
break ;
}
}
document.write( count );
}
var N = 4;
var A = [1, 2, 3, 4];
var B = [1, 2, 4, 3];
maximumCount(A, B, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...