<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>

<p>A subarray is called <strong>equal</strong> if all of its elements are equal. Note that the empty subarray is an <strong>equal</strong> subarray.</p>

<p>Return <em>the length of the <strong>longest</strong> possible equal subarray after deleting <strong>at most</strong> </em><code>k</code><em> elements from </em><code>nums</code>.</p>

<p>A <b>subarray</b> is a contiguous, possibly empty sequence of elements within an array.</p>

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

<pre>
<strong>Input:</strong> nums = [1,3,2,3,1,3], k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> It&#39;s optimal to delete the elements at index 2 and index 4.
After deleting them, nums becomes equal to [1, 3, 3, 3].
The longest equal subarray starts at i = 1 and ends at j = 3 with length equal to 3.
It can be proven that no longer equal subarrays can be created.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,2,2,1,1], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> It&#39;s optimal to delete the elements at index 2 and index 3.
After deleting them, nums becomes equal to [1, 1, 1, 1].
The array itself is an equal subarray, so the answer is 4.
It can be proven that no longer equal subarrays can be created.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= nums.length</code></li>
	<li><code>0 &lt;= k &lt;= nums.length</code></li>
</ul>

class SegTree:
    def __init__(self, left, right, v):
        self.leftMost = left
        self.rightMost = right
        if self.leftMost == self.rightMost:
            self.res = v[left]
            self.leftChild = None
            self.rightChild = None
        else:
            mid = (left + right) // 2
            self.leftChild = SegTree(self.leftMost, mid, v)
            self.rightChild = SegTree(mid + 1, self.rightMost, v)
            self.recalc()

    def recalc(self):
        if self.leftMost == self.rightMost:
            return
        self.res = max(self.leftChild.res, self.rightChild.res)

    def update(self, index, newVal):
        if self.leftMost == self.rightMost:
            self.res += newVal
            return

        if index <= self.leftChild.rightMost:
            self.leftChild.update(index, newVal)
        else:
            self.rightChild.update(index, newVal)
        self.recalc()

    def query(self, l, r):
        if l > self.rightMost or r < self.leftMost:
            return 0
        if l <= self.leftMost and r >= self.rightMost:
            return self.res
        return max(self.leftChild.query(l, r), self.rightChild.query(l, r))


class Solution:
    def longestEqualSubarray(self, nums, k):
        n = len(nums)
        MAX = max(nums)
        arr = [0] * (MAX + 1)
        st = SegTree(0, MAX, arr)
        l, res = 0, 0

        for r in range(n):
            st.update(nums[r], 1)
            while (r - l + 1) - st.query(0, MAX) > k:
                st.update(nums[l], -1)
                l += 1
            res = max(st.query(0, MAX), res)

        return res

56200