Skip to content
Onsite

Closest Smaller Element

MediumP3Coding

For each element of nums, find the closest element (by index distance) that is strictly smaller than it. If a smaller element exists on both sides at the same distance, take the one on the left. If no element is smaller, the answer is -1.

Return the value found for each position.

Example 1

Input
nums = [3,1,2]
Output
[1,-1,1]

Example 2

Input
nums = [4,4,4]
Output
[-1,-1,-1]

Example 3

Input
nums = [1,2,3]
Output
[-1,1,2]

Constraints

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

Hints

Run checks the 3 visible tests, Submit all 11.