Find the Longest Equal Subarray

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
Inputnums = [1,3,2,3,1,3], k = 3
Output3
It's optimal to delete the elements at index 2 and index 4, making the longest equal subarray [3, 3, 3] of length 3.
Example 2
Inputnums = [1,1,2,2,1,1], k = 2
Output4
It's optimal to delete the elements at index 2 and index 3, leaving [1, 1, 1, 1], so the answer is 4.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= nums.length
  • 0 <= k <= nums.length

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate