Find the first element greater than everything on its left and smaller than everything on its right

Quick Overview

Find the first element of an integer array that is greater than everything before it and smaller than everything after it, or return -1 when none exists. It tests prefix maximum and suffix minimum reasoning, a linear-time scan, and careful handling of strict comparisons and array boundaries.

Find the first element greater than everything on its left and smaller than everything on its right

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer array `nums`, find an element such that every element to its left is strictly smaller than it and every element to its right is strictly greater than it. If several elements qualify, return the first one (the one with the smallest index). If no element qualifies, return `-1`. The first element has nothing to its left, so the left condition holds for it automatically. The last element has nothing to its right, so the right condition holds for it automatically. ### Function Signature ```python def find_partition_point(nums: list[int]) -> int: ``` ### Rules - Return the value of the qualifying element, not its index. - Both comparisons are strict. An element that equals any element on its left or on its right does not qualify. - An array of length 1 returns its only element. - Return `-1` when no element qualifies. Since `-1` can also be a value in `nums`, an output of `-1` does not by itself say which case happened, but the expected output for every input is still unique. ### Constraints - `1 <= len(nums) <= 10^5` - `-10^9 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [4, 2, 5, 7, 6, 8, 9] Output: 5 ``` The 5 at index 2 has [4, 2] on its left, all smaller, and [7, 6, 8, 9] on its right, all greater. The two earlier elements fail: 4 has a 2 to its right, and 2 has a 4 to its left. The 8 and the 9 also qualify, but 5 comes first. ```text Input: nums = [5, 1, 4, 3, 6, 8, 10, 7, 9] Output: 6 ``` The 6 at index 4 has [5, 1, 4, 3] on its left, whose largest value is 5, and [8, 10, 7, 9] on its right, whose smallest value is 7. Every element before it fails: 5 has a 1 to its right, and 1, 4 and 3 each have a 5 to their left. ```text Input: nums = [3, 1, 2] Output: -1 ``` The 3 has smaller values to its right, the 1 has a larger value to its left, and the 2 has a 3 to its left, so no element qualifies.

Overview: Find the first element of an integer array that is greater than everything before it and smaller than everything after it, or return -1 when none exists. It tests prefix maximum and suffix minimum reasoning, a linear-time scan, and careful handling of strict comparisons and array boundaries.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums, find an element such that every element to its left is strictly smaller than it and every element to its right is strictly greater than it.

If several elements qualify, return the first one (the one with the smallest index). If no element qualifies, return -1.

The first element has nothing to its left, so the left condition holds for it automatically. The last element has nothing to its right, so the right condition holds for it automatically.

Function Signature

def find_partition_point(nums: list[int]) -> int:

Rules

  • Return the value of the qualifying element, not its index.
  • Both comparisons are strict. An element that equals any element on its left or on its right does not qualify.
  • An array of length 1 returns its only element.
  • Return -1 when no element qualifies. Since -1 can also be a value in nums , an output of -1 does not by itself say which case happened, but the expected output for every input is still unique.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9

Examples

Input: nums = [4, 2, 5, 7, 6, 8, 9]
Output: 5

The 5 at index 2 has [4, 2] on its left, all smaller, and [7, 6, 8, 9] on its right, all greater. The two earlier elements fail: 4 has a 2 to its right, and 2 has a 4 to its left. The 8 and the 9 also qualify, but 5 comes first.

Input: nums = [5, 1, 4, 3, 6, 8, 10, 7, 9]
Output: 6

The 6 at index 4 has [5, 1, 4, 3] on its left, whose largest value is 5, and [8, 10, 7, 9] on its right, whose smallest value is 7. Every element before it fails: 5 has a 1 to its right, and 1, 4 and 3 each have a 5 to their left.

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

The 3 has smaller values to its right, the 1 has a larger value to its left, and the 2 has a 3 to its left, so no element qualifies.