Sunday, January 29, 2023
HomeSoftware DevelopmentDepend pair of indices in Array having equal Prefix-MEX and Suffix-MEX

Depend pair of indices in Array having equal Prefix-MEX and Suffix-MEX


  

#embrace <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;

  

    

    

    vector<int> B(n);

  

    

    

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

        

        s.insert(i);

    }

  

    

    

    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;

}

  

void countPairs(vector<int>& arr, int N)

{

  

    

    

    

    int depend = 0;

  

    

    

    vector<int> P = Prefix_MEX(arr, N);

  

    

    reverse(arr.start(), arr.finish());

  

    

    

  

    vector<int> S = Prefix_MEX(arr, N);

  

    

    

    reverse(S.start(), S.finish());

  

    

    

    map<int, int> mp;

  

    

    

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

        mp[P[i]]++;

    }

  

    

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

  

        

        

        if (mp.discover(S[i]) == mp.finish()) {

            proceed;

        }

  

        

        

        else {

            depend += mp[S[i]];

        }

    }

    cout << depend << endl;

}

  

int foremost()

{

    vector<int> arr = { 1, 0, 2, 0, 1 };

    int N = arr.measurement();

  

    

    countPairs(arr, N);

    arr = { 1, 2, 3 };

    N = arr.measurement();

  

    

    countPairs(arr, N);

  

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments