Maximum Score of a Good Subarray

HardTwo Pointerstwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an array of positive integers nums and an index k. The **score** of a subarray nums[i..j] is min(nums[i..j]) * (j - i + 1). A subarray is **good** if it contains index k, i.e. i <= k <= j. Return the **maximum score** of any good subarray. Aim for O(n) time.

Examples

Input: nums = [3,5,4,1,6], k = 2
Output: 9
The subarray `[3,5,4]` (indices 0-2) contains index 2, has minimum 3 and length 3, for a score of 9.
Input: nums = [2,2,2], k = 0
Output: 6
The whole array has minimum 2 and length 3.

Constraints

  • `1 <= nums.length <= 10^5`
  • `1 <= nums[i] <= 2 * 10^4`
  • `0 <= k < nums.length`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems