Find the maximum length of a subarray that can be made entirely equal after deleting at most elements from it.
Longest Equal Subarray
gfgYou are given an integer array nums and an integer k.
Choose a subarray of nums. You may delete at most k elements from inside that chosen subarray. After the deletions, the remaining elements must all be equal.
Return the maximum possible length of the remaining equal subarray.
In other words, for some value x, you want to find a contiguous segment where, after removing up to k non-x elements, all remaining elements are x, and the number of retained elements is as large as possible.
nums: an integer arrayk: a non-negative integerk deletions within one chosen subarray.1 <= nums.length <= $10^{5}$1 <= nums[i] <= $10^{5}$0 <= k <= nums.lengthnumskk elements from one chosen subarray1 <= nums.length <= $10^{5}$1 <= nums[i] <= $10^{5}$0 <= k <= nums.lengthExample 1
Input
nums = [1,3,2,3,1,3], k = 2
Output
3
Explanation
Choose the subarray [3,2,3,1,3]. Delete 2 and 1, leaving [3,3,3] of length 3.
Example 2
Input
nums = [1,1,2,2,1,1], k = 2
Output
4
Explanation
Choose the whole array. Delete the two 2s, leaving [1,1,1,1].
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.