Ctrl K

Low, Peak, Middle

Medium
You are given an integer array values. Look for three elements, not necessarily adjacent but in this order, values[i], values[j], values[k] with i < j < k, such that values[i] < values[k] < values[j]. In other words, a smaller value comes first, then the largest of the three, and the last one falls strictly between them. Return True if such a triple exists and False otherwise.
Example 1
values=[6, 2, 9, 5] → True

Take 2, 9, 5: a small value, then the largest one, then one that lands between them.

Example 2
values=[9, 7, 4, 4, 1] → False

The values only go down, so nothing later is ever larger and there is no peak to build on.

Example 3
values=[3, 8, 3, 10] → False

3, 8, 3 does not count: the third value must be strictly greater than the first. No other triple works either.

Constraints
  • 1 ≤ len(values) ≤ 2 · 10⁵
  • -10⁹ ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCut the Middle to Restore Order MediumFrog on the Posts HardWidest Fence Span Around the Marked Board HardSecond Larger to the Right HardBest Pair Above the Bars Hard
loading Python…
Input
values=[6, 2, 9, 5]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 146 tests on the server, hidden ones included.