You are given a 0-indexed integer array nums and an integer k.
A subarray is called equal if all of its elements are equal. Note that the empty subarray is an equal subarray.
Return the length of the longest possible equal subarray after deleting at most k elements from nums.
A subarray is a contiguous, possibly empty sequence of elements within an array.
Function Signature
def longest_equal_subarray(nums: list[int], k: int) -> int:
Rules
-
Deleting an element removes it from the array, so the elements on either side of it become adjacent.
-
Deletions can be taken from anywhere in
nums
, and you may use fewer than
k
of them, including none.
-
Return only the length of the longest equal subarray in the array that remains.
Constraints
-
1 <= len(nums) <= 10^5
-
1 <= nums[i] <= len(nums)
-
0 <= k <= len(nums)
Examples
Input: nums = [1,3,2,3,1,3], k = 3
Output: 3
Delete the elements at indices 0, 2 and 4 (values 1, 2, 1). The array becomes [3, 3, 3], and since nums holds only three 3s, no equal subarray can be longer.
Input: nums = [1,1,2,2,1,1], k = 2
Output: 4
Delete the two 2s at indices 2 and 3. The array becomes [1, 1, 1, 1], an equal subarray of length 4.
Input: nums = [1,2,2,1,1,1], k = 0
Output: 3
With no deletions allowed, the answer is the longest run already present: the three 1s at indices 3 to 5.