Ctrl K

Same-Value Run After Removals

Medium
You are given an integer array values and an integer removals. You may delete at most removals elements from the array (any of them; the rest keep their order). Afterwards, look for the longest contiguous block whose elements are all equal. Return the greatest length of such a block you can achieve with the best choice of deletions.
Example 1
values=[4, 2, 4, 3, 3, 4, 4], removals=2 → 3

Removing both 3s (positions 3 and 4) gives [4, 2, 4, 4, 4], which has three 4s in a row. Getting four 4s together would also need the 2 removed, which is three removals.

Example 2
values=[1, 2, 3], removals=0 → 1

Nothing can be removed and all numbers differ, so the longest block of equal values has length 1.

Example 3
values=[2, 1, 2, 1, 2], removals=2 → 3

Remove both 1s to get [2, 2, 2].

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁹
  • 0 ≤ removals ≤ len(values)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Separate Segments With a Given Sum MediumTrim the Ends to a Target MediumA Run With No Gaps HardMedian of Window Variety HardShortest Stretch to the Goal Medium
loading Python…
Input
values=[4, 2, 4, 3, 3, 4, 4], removals=2
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 123 tests on the server, hidden ones included.