← All questions
MediumCoding

Count Subarrays Where Max Element Appears at Least K Times

Asked atMeta
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Find the global maximum value.
  2. 02Expand a right pointer and count max occurrences.
  3. 03Shrink left while count is at least k.
  4. 04Add left to the answer after shrinking.
  5. 05Use a wide answer type.

Reference answer

Then expect these follow-ups

  • Why does adding left count all valid starts?

    Tests: correctness reasoning

  • How would you count subarrays with exactly k maxima?

    Tests: follow-up reasoning

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question