Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Maximum Score of a Good Subarray

XPChallenge Points: 30
levelLevel: Hard

You are given an integer array nums and an index k.
We want to evaluate all subarrays that must include the index k. For any chosen subarray (i, j) where i ≤ k ≤ j, its score is calculated as:

score = minimum value within nums[i..j] × length of the subarray

Your task is to determine the highest score obtainable among all such subarrays that contain index k.
The subarray can expand to the left or right, but it must always cover position k.

Example 1:

Input: nums = [3, 1, 5, 6, 2, 4], k = 2

Output: 10

Explanation:

The best subarray is (2, 3) → values [5, 6] Minimum = 5, length = 2 → score = 5 × 2 = 10.

Example 2:

Input: nums = [9, 7, 3, 8, 4], k = 4

Output: 12

Explanation:

The optimal subarray is (3, 4) → values [8, 4] Minimum = 4, length = 2 → score = 8 But (2,4) → values [3,8,4] Minimum = 3, length = 3 → score = 9 The best is (0,4) → min = 3, length = 5 → score 15 → actually max is 15. (Updated example: Output = 15)

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result