<p>Given an array <code>arr</code> that represents a permutation of numbers from <code>1</code> to <code>n</code>.</p>

<p>You have a binary string of size <code>n</code> that initially has all its bits set to zero. At each step <code>i</code> (assuming both the binary string and <code>arr</code> are 1-indexed) from <code>1</code> to <code>n</code>, the bit at position <code>arr[i]</code> is set to <code>1</code>.</p>

<p>You are also given an integer <code>m</code>. Find the latest step at which there exists a group of ones of length <code>m</code>. A group of ones is a contiguous substring of <code>1</code>&#39;s such that it cannot be extended in either direction.</p>

<p>Return <em>the latest step at which there exists a group of ones of length <strong>exactly</strong></em> <code>m</code>. <em>If no such group exists, return</em> <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [3,5,1,2,4], m = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> 
Step 1: &quot;00<u>1</u>00&quot;, groups: [&quot;1&quot;]
Step 2: &quot;0010<u>1</u>&quot;, groups: [&quot;1&quot;, &quot;1&quot;]
Step 3: &quot;<u>1</u>0101&quot;, groups: [&quot;1&quot;, &quot;1&quot;, &quot;1&quot;]
Step 4: &quot;1<u>1</u>101&quot;, groups: [&quot;111&quot;, &quot;1&quot;]
Step 5: &quot;111<u>1</u>1&quot;, groups: [&quot;11111&quot;]
The latest step at which there exists a group of size 1 is step 4.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [3,1,5,4,2], m = 2
<strong>Output:</strong> -1
<strong>Explanation:</strong> 
Step 1: &quot;00<u>1</u>00&quot;, groups: [&quot;1&quot;]
Step 2: &quot;<u>1</u>0100&quot;, groups: [&quot;1&quot;, &quot;1&quot;]
Step 3: &quot;1010<u>1</u>&quot;, groups: [&quot;1&quot;, &quot;1&quot;, &quot;1&quot;]
Step 4: &quot;101<u>1</u>1&quot;, groups: [&quot;1&quot;, &quot;111&quot;]
Step 5: &quot;1<u>1</u>111&quot;, groups: [&quot;11111&quot;]
No group of size 2 exists during any step.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == arr.length</code></li>
	<li><code>1 &lt;= m &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= arr[i] &lt;= n</code></li>
	<li>All integers in <code>arr</code> are <strong>distinct</strong>.</li>
</ul>

struct UnionFind {

    public:
        UnionFind() : par(0, 0), rank(0, 0), siz(0, 1), number{0}, group_number{0} {}
        UnionFind(int n) : par(n, -1), rank(n, 0), siz(n, 1), number{n}, group_number{n} {
            iota(par.begin(), par.end(), 0);
        }

        int root(int vertex, int index = 0) {

            if (par[vertex] != vertex) {
                par[vertex] = root(par[vertex]);
            }

            return par[vertex] + index;
        }

        vector<int> all_roots(int index = 0) {

            vector<int> results;

            for (int i = 0; i < number; i++) {
                if (root(i) == i) results.push_back(i + index);
            }

            return results;
        }

        bool is_same_root(int vertex1, int vertex2) {
            return root(vertex1) == root(vertex2);
        }

        bool unite(int reference_vertex, int vertex) {

            int root_x = root(reference_vertex);
            int root_y = root(vertex);

            if (root_x == root_y) {
                return false;
            }

            --group_number;

            if (rank[root_x] < rank[root_y]) {
                swap(root_x, root_y);
            }

            par[root_y] = root_x;
            siz[root_x] += siz[root_y];

            if (rank[root_x] == rank[root_y]) {
                ++rank[root_x];
            }

            return true;
        }

        int root_size(int vertex) {
            return siz[root(vertex)];
        }

        vector<int> all_root_size() {

            vector<int> results;
            for (int vertex = 0; vertex < number; vertex++) {
                if (root(vertex) == vertex) {
                    results.push_back(siz[root(vertex)]);
                }
            }

            return results;
        }

        int group_count() {
            return group_number;
        }

        vector<int> same_group_members(int vertex, int index = 0) {

            vector<int> results;
            for (int i = 0; i < number; i++) {
                if (root(i) == root(vertex)) {
                    results.push_back(i + index);
                }
            }

            return results;
        }

        unordered_map<int, vector<int>> all_group_members(int index = 0) {

            unordered_map<int, vector<int>> results;

            for (int i = 0; i < number; i++) {
                results[root(i)].push_back(i + index);
            }

            return results;
        }

        friend ostream& operator<<(ostream& os, UnionFind& uf) {

            for (auto [root, group_members] : uf.all_group_members()) {
                os << "root vertex:" << root << " - [";
                for (int i = 0; i < (int) group_members.size(); i++) {
                    if (i) {
                        os << ", ";
                    }
                    os << group_members[i];
                }
                os << "]";
                os << '\n';
            }

            return os;
        }

    private:
        vector<int> par;
        vector<int> rank;
        vector<int> siz;
        int number;
        int group_number;
};

class Solution {
public:
    int findLatestStep(vector<int>& arr, int m) {

        int n = arr.size();
        UnionFind uf{n + 2};
        vector<int> bits(n + 2, 0);
        vector<int> counter(n + 1, 0);
        counter[0] = n;
        int ans = -1;

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

            int v = arr[i];
            if (bits[v]) {

                if (0 < counter[m]) {
                    ans = max(ans, i + 1);
                }
                continue;
            }

            bits[v] = 1;
            --counter[0];
            if (bits[v - 1] && bits[v + 1]) {
                --counter[uf.root_size(v - 1)];
                --counter[uf.root_size(v + 1)];
                uf.unite(v, v + 1);
                uf.unite(v, v - 1);
                ++counter[uf.root_size(v)];
            } else if (bits[v + 1]) {
                --counter[uf.root_size(v + 1)];
                uf.unite(v, v + 1);
                ++counter[uf.root_size(v)];
            } else if (bits[v - 1]) {
                --counter[uf.root_size(v - 1)];
                uf.unite(v, v - 1);
                ++counter[uf.root_size(v)];
            } else {
                ++counter[1];
            }

            if (0 < counter[m]) {
                ans = max(ans, i + 1);
            }
        }

        return ans;
    }
};
94600