132 Pattern

Medium Topics Company Tags

Return true if there are indices i < j < k such that nums[i] < nums[k] < nums[j]. The three selected values do not need to be adjacent. Otherwise return false.

Example 1:

Input: nums = [1,4,2]

Output: true

Explanation: Use indices (0, 1, 2): their values satisfy 1 < 2 < 4, so they form a 132 pattern.

Example 2:

Input: nums = [4,3,2,1]

Output: false

Explanation: The array strictly decreases, so the first selected value can never be smaller than the third selected value.

Example 3:

Input: nums = [1,2,2]

Output: false

Explanation: The only triple has equal second and third values. The inequalities must be strict, so it is not a 132 pattern.

Constraints:

  • 1 <= nums.length <= 200000
  • -1000000000 <= nums[i] <= 1000000000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.