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

from sortedcontainers import SortedList
class Solution:
    def findLatestStep(self, arr: List[int], m: int) -> int:
        # array has numbers from 1 to n, shuffled
        # binary string of size n, all bits set to 0
        # at each step i the bit at position arr[i] is set to 1

        # m = find the latest step at which there is a group of ones of length m
        # group is cintiguours, it can't be extended in either direction
        # latest step of group of 1 of length m, 


        # at wach step the number of 1 is going to grow, so our contiguous string is increasing

        # the group of size 2 will dissapear
        # we can check if it exists in linear time

        n = len(arr)
        if n == m:
            return n
        if m > n:
            return -1

        # for each
        # [3,5,1,2,4]
        # for each i we keep track of everything that ends at n - 1 starts at n + 1
        # 11101
        # 3 ending at 2, ending at 4

        #  idx - 1  3 idx idx + 1



        # 00000 - 0 0 0 0 0 0 0 
        # 00100 - 0 0 0 1 0 0 0
        # 00101 - 0 0 0 1 0 1 0
        # 10101 - 0 1 0 1 0 1 0
        # 11101 - 0 5 3 3 5 5 0
        res = -1

        cnt = [0]*(n+1)
        lns = [0]*(n+2) # lns[i] represents length of the sequence that starts or ends at i

        for i, val in enumerate(arr):
            left, right = lns[val-1], lns[val+1]
            new_len = 1 + left + right
            
            lns[val] = new_len
            lns[val-left] = new_len
            lns[val+right] = new_len

            cnt[new_len] += 1
            cnt[left] -= 1
            cnt[right] -= 1

            if cnt[m] > 0:
                res = i + 1
    

        return res
31000