FastPrepFirst Value Below Both Neighbors

First Value Below Both Neighbors

Capital One logoCapital One● EasyFULLTIMEOA
Learn

Problem statement

Given an integer array nums, inspect its interior elements from left to right. Return the value of the first element whose immediate left and right neighbors are both strictly larger.

Return -1 when no such interior element exists. The first and last elements are not eligible because they do not have two adjacent neighbors.

Function

firstValleyValue(nums: int[]) → int

Examples

Example 1

nums = [8,3,5,2,4]return = 3

The value 3 is below both 8 and 5. Although 2 is also below both neighbors, it appears later.

Example 2

nums = [1,2,3]return = -1

The only interior value is not below either neighbor.

Example 3

nums = [5,1,1,5]return = -1

Equal adjacent values do not satisfy the strict comparison.

Constraints

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

More Capital One problems

See Capital One hiring insights
public int firstValleyValue(int[] nums) {
    // Write your code here.
}
nums[8,3,5,2,4]
expected3
Checking account…