Count of Array Elements whose Order of Deletion Precedes Order of Insertion


In this problem, we will count the number of elements removed from the array before they are inserted in the array.

The logical part for solving the problem is that check for all numbers that its position in the remove[] array before its position in the insert[] array. If yes, we can count that particular element of the remove[] array in the answer.

However, we will use the map data structure to improve the performance of the code.

Problem statement − We have given an insert[] and remove[] arrays containing first N integers. The insert[] array represents the order of 1 to N elements in which we have inserted elements into the array, and the remove[] array represents the order of 1 to N elements in which elements are removed. We need to find the number of elements that are removed before insertion.

Sample examples

Input

insert[] = {1, 2, 5, 3, 6, 4}, remove[] = {1, 3, 4, 5, 6, 2};

Output

2

Explanation − The 3 and 4 are removed before they are inserted in the array.

Input

insert[] = {4, 3, 2, 1}, remove[] = {4, 3, 2, 1}

Output

0

Explanation − All elements are removed after insertion.

Input

insert[] = {1, 2, 3, 4, 5, 6}, remove[] = {2, 3, 4, 5, 6, 1};

Output

5

Explanation − All elements are removed before insertion except 1.

Approach 1

This approach will use two nested loops to traverse the insert[] and remove[] array. For each element of the insert[] array, we will check its position in the remove[] array and count the number of remove[q] == insert[p] and q < p

Algorithm

Step 1 − Initialize the ‘cnt’ with 0 to store the number of elements.

Step 2 − Use the for loop to traverse the insert[] array. Also, use another nested for loop to traverse the remove[] array.

Step 3 − If the element at index p in the insert[] array and the element at index q in the remove[] array is the same, and q is less than p, increment the ‘cnt’ value by 1.

Step 4 − Else, if insert[p] and remove[q] is the same but q is not less than or equal to p, break the loop.

Step 5 − Print the ‘cnt’ value.

Example

#include <bits/stdc++.h>
using namespace std;

int findMaximumCnt(int insert[], int remove[], int n) {
    int cnt = 0;
    // Traverse insert[] and remove[] array and find position of each elements
    for (int p = 0; p < n; p++) {
        for (int q = 0; q < n; q++) {
            if (insert[p] == remove[q] && q < p) {
                cnt++;
            } else if (insert[p] == remove[q]) {
                break;
            }
        }
    }
    return cnt; // Return the total number of elements removed before insertion
}

int main() {
    int N = 6;
    int insert[] = {1, 2, 5, 3, 6, 4};
    int remove[] = {1, 3, 4, 5, 6, 2};
     cout << "The total number of elements removed before insertion is " << findMaximumCnt(insert, remove, N) << endl;
    return 0;
}

Output

The total number of elements removed before insertion is 2

Approach 2

In this approach, we will use the map data structure. We will store the position of each element of the insert[] array into the map. After that, we will traverse the remove[] array and compare its each element’s position with the insert[] array’s position.

Algorithm

Step 1 − Define the map to store the key and value of the integer type.

Step 2 − Insert all elements of the insert[] array into the map with the element’s index value.

Step 3 − Initialize the ‘cnt’ with 0.

Step 4 − Traverse the remove[] array.

Step 5 − If index p is less than mp[remove[p]], increment the ‘cnt’ value.

Step 6 − Print the ‘cnt’ value.

Example

#include <bits/stdc++.h>
using namespace std;

int findMaximumCnt(int insert[], int remove[], int n) {
    // Map to store elements of insert[] array
    map<int, int> mp;
    // Insert elements into the map
    for (int p = 0; p < n; p++) {
        mp[insert[p]] = p;
    }
    int cnt = 0;
    // Count elements of remove[] array, whose index in the insert[] array is large
    for (int p = 0; p < n; p++) {
        if (p < mp[remove[p]]) {
            cnt++;
        }
    }
    return cnt;
}
int main() {
    int N = 6;
    int insert[] = {1, 2, 5, 3, 6, 4};
    int remove[] = {1, 3, 4, 5, 6, 2};
    cout << "The total number of elements removed before insertion is " << findMaximumCnt(insert, remove, N) << endl;
    return 0;
}

Output

The total number of elements removed before insertion is 2

Time complexity − O(N) for traversing the array.

Space complexity − O(N) for using the map.

The second approach gives better performance than the first approach, but it takes more memory. However, programmers may use the queue data structure to solve the problem.

Updated on: 02-Aug-2023

38 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements