Saturday, December 9, 2023
HomeSoftware DevelopmentDiscover the Prefix-MEX Array for given Array

Discover the Prefix-MEX Array for given Array


Given an array A[] of N components, the duty is to create a Prefix-MEX array for this given array. Prefix-MEX array B[] of an array A[] is created such that MEX of A[0] until A[i] is B[i]

MEX of an array refers back to the smallest lacking non-negative integer of the array.

Examples:

Enter: A[] = {1, 0, 2, 4, 3}
Output: 0 2 3 3 5
Rationalization: Within the array A, components 
Until 1st index, components are [1] and mex until 1st index is 0.
Until 2nd index, components are [1, 0] and mex until 2nd index is 2.
Until third index, components are [ 1, 0, 2] and mex until third index is 3.
Until 4th index, components are [ 1, 0, 2, 4] and mex until 4th index is 3.
Until fifth index, components are [ 1, 0, 2, 4, 3] and mex until fifth index is 5.
So our last array B can be [0, 2, 3, 3, 5].

Enter: A[] = [ 1, 2, 0 ]
Output: [ 0, 0, 3 ]
Rationalization: Within the array A, components 
Until 1st index, components are [1] and mex until 1st index is 0.
Until 2nd index, components are [1, 2] and mex until 2nd index is 0.
Until third index, components are [ 1, 2, 0] and mex until third index is 3.
So our last array B can be [0, 0, 3].

 

Naive Strategy: The only option to remedy the issue is:

For every aspect at ith (0 ≤ i < N)index of the array A[], discover MEX from 0 to i and retailer it at B[i].

Comply with the steps talked about beneath to implement the concept:

  • Iterate over the array from i = 0 to N-1:
    • For each ith index in array A[]
  • Return the resultant array B[] on the finish.

Time Complexity: O(N2)
Auxiliary House: O(N)

Environment friendly Strategy: This strategy relies on the utilization of Set information construction.

A set shops information in sorted order. We will benefit from that and retailer all of the non-negative integers until the utmost worth of the array. Then traverse by means of every array aspect and take away the visited information from set. The smallest remaining aspect would be the MEX for that index.

Comply with the steps beneath to implement the concept:

  • Discover the utmost aspect of the array A[].
  • Create a set and retailer the numbers from 0 to the utmost aspect within the set.
  • Traverse by means of the array from i = 0 to N-1
    • For every aspect, erase that aspect from the set.
    • Now discover the smallest aspect remaining within the set.
    • That is the prefix MEX for the ith aspect. Retailer this worth within the resultant array.
  • Return the resultant array because the required reply.

Under is the implementation of the above strategy. 

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

vector<int> Prefix_Mex(vector<int>& A, int n)

{

    

    int mx_element = *max_element(A.start(), A.finish());

  

    

    

    set<int> s;

    for (int i = 0; i <= mx_element + 1; i++) {

        s.insert(i);

    }

  

    

    vector<int> B(n);

    for (int i = 0; i < n; i++) {

  

        

        auto it = s.discover(A[i]);

  

        

        if (it != s.finish())

            s.erase(it);

  

        

        

        B[i] = *s.start();

    }

  

    

    return B;

}

  

int principal()

{

  

    vector<int> A = { 1, 0, 2, 4, 3 };

    int N = A.dimension();

  

    

    vector<int> B = Prefix_Mex(A, N);

  

    

    for (int i = 0; i < N; i++) {

        cout << B[i] << " ";

    }

    return 0;

}

Time Complexity: O(N * log N )

  • O(N) for iterating the vector, and 
  • O(log N) for inserting and deleting the aspect from the set.

Auxiliary House: O(N)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments