132 Pattern

Given an array nums, determine whether there exists a subsequence of three integers nums[i], nums[j], nums[k] such that i < j < k and nums[i] < nums[k] < nums[j]. Example: Input: nums = [3,1,4,2] Output: true Explanation: The subsequence (1, 4, 2) satisfies the 132 pattern. Pattern focus: Monotonic Stack for pattern detection.

Input Format

nums = array of integers

Output Format

true if a 132 pattern exists; otherwise false

Constraints

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

Examples

Example 1:

Input:

nums = [3,1,4,2]

Output:

true

Explanation:

The subsequence 1,4,2 matches the required ordering.

Example 2:

Input:

nums = [1,2,3,4]

Output:

false

Explanation:

No subsequence can satisfy nums[i] < nums[k] < nums[j].

Loading...
132 Pattern - Monotonic Stack Queue DSA Problem