2831. Find the Longest Equal Subarray
Medium37.6% acceptance29,834 / 79,244 submissions
Asked by 3 companies
Topics
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.
Example 1:
Input: nums = [1,3,2,3,1,3], k = 3 Output: 3 Explanation: It'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.
Example 2:
Input: nums = [1,1,2,2,1,1], k = 2 Output: 4 Explanation: It'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.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= nums.length0 <= k <= nums.length
Hints
Hint 1
For each number
x in nums, create a sorted list indicesx of all indices i such that nums[i] == x.Hint 2
On every
indicesx, execute a sliding window technique.Hint 3
For each
indicesx, find i, j such that (indicesx[j] - indicesx[i]) - (j - i) <= k and j - i + 1 is maximized.Hint 4
The answer would be the maximum of
j - i + 1 for all indicesx.