<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>

class DSU{
    vector<int> parent;
    vector<int> size;

public:
    DSU(int n) {
        parent.resize(n);
        size.assign(n, 1);
        for(int i=0; i<n; i++) parent[i] = i;
    }

    int findParent(int node) {
        if(node == parent[node]) return node;
        return parent[node] = findParent(parent[node]);
    }

    void unify(int u, int v) {
        int parent_u = findParent(u);
        int parent_v = findParent(v);

        if(parent_u == parent_v) return;

        if(size[parent_u] >= size[parent_v]) {
            parent[parent_v] = parent_u;
            size[parent_u] += size[parent_v];
        } else {
            parent[parent_u] = parent_v;
            size[parent_v] += size[parent_u];
        }
        return;
    }
};

class Solution {
public:
    int findLatestStep(vector<int>& arr, int m) {
        int n = arr.size();
        DSU dsu(n);
        vector<int> binaryArray(n, 0);
        unordered_map<int, int> sizeMap;

        int count = 0, ans = -1;

        for(int step = 0; step < n; step++) {
            int i = arr[step] - 1;
            // Both prev and next is 1
            if(i > 0 && binaryArray[i - 1] == 1 && i + 1 < n && binaryArray[i + 1] == 1) {
                int parent_i_plus_1 = dsu.findParent(i + 1);
                int parent_i_minus_1 = dsu.findParent(i - 1);

                int i_plus_1_size = sizeMap[parent_i_plus_1];
                int i_minus_size = sizeMap[parent_i_minus_1];
                
                if(i_plus_1_size == m) count--;
                sizeMap.erase(parent_i_plus_1);

                if(i_minus_size == m) count--;
                sizeMap.erase(parent_i_minus_1);

                dsu.unify(i, i+1);
                dsu.unify(i, i-1);

                sizeMap[dsu.findParent(i)] = i_plus_1_size + i_minus_size + 1;
                if(i_plus_1_size + i_minus_size + 1 == m) count++;
            }
            // Prev is 1
            else if(i > 0 && binaryArray[i - 1] == 1) {
                int parent_i_minus_1 = dsu.findParent(i - 1);
                int i_minus_size = sizeMap[parent_i_minus_1];

                if(i_minus_size == m) count--;
                sizeMap.erase(parent_i_minus_1);

                dsu.unify(i, i-1);

                sizeMap[dsu.findParent(i)] = i_minus_size + 1;
                if(i_minus_size + 1 == m) count++;
            }
            // Next is 1
            else if(i + 1 < n && binaryArray[i + 1] == 1) {
                int parent_i_plus_1 = dsu.findParent(i + 1);
                int i_plus_1_size = sizeMap[parent_i_plus_1];
                
                if(i_plus_1_size == m) count--;
                sizeMap.erase(parent_i_plus_1);

                dsu.unify(i, i+1);

                sizeMap[dsu.findParent(i)] = i_plus_1_size + 1;
                if(i_plus_1_size + 1 == m) count++;
            } else {
                sizeMap[i] = 1;
                if(m == 1) count++;
            }

            if(count > 0) {
                ans = step + 1;
            }
            binaryArray[i] = 1;
        }

        return ans;
    }
};
121500