FastPrepSum Strict Local Maxima

Sum Strict Local Maxima

Visa logoVisa● EasyINTERNOA
Learn

Problem statement

You are given an integer array nums.

An element at index i is a strict local maximum when it has two neighbors and is greater than both of them: nums[i] > nums[i - 1] and nums[i] > nums[i + 1].

Return the sum of all strict local maxima. The first and last elements are never considered because they do not have two neighbors.

Function

sumStrictLocalMaxima(nums: int[]) → long

Examples

Example 1

nums = [1,5,2,4,3]return = 9

5 is greater than 1 and 2, and 4 is greater than 2 and 3. Their sum is 9.

Example 2

nums = [5,1,4]return = 0

The only eligible element is 1, which is smaller than both neighbors. The endpoints are not eligible.

Constraints

  • 3 <= nums.length <= 105
  • -109 <= nums[i] <= 109
  • The answer fits in a signed 64-bit integer.

More Visa problems

See Visa hiring insights
public long sumStrictLocalMaxima(int[] nums) {
    // Write your code here.
}
nums[1,5,2,4,3]
expected9
Checking account…