Find the Number of Subarrays Where Boundary Elements Are Maximum

You are given an array of positive integers nums.

Return the number of subarrays of nums where the first and the last elements of the subarray are equal to the largest element in the subarray.

Example 1
Inputnums = [1,4,3,3,2]
Output6
There are 6 qualifying subarrays: each single-element subarray and the subarray [3, 3].
Example 2
Inputnums = [3,3,3]
Output6
All 6 subarrays have first and last elements equal to the largest element, since every element is 3.

Constraints

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

Asked at 2 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate