132 Pattern - Problem
Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].
Return true if there is a 132 pattern in nums, otherwise return false.
Input & Output
Example 1 — Found Pattern
$
Input:
nums = [1,2,3,4]
›
Output:
false
💡 Note:
No 132 pattern exists. The array is strictly increasing, so no element can satisfy nums[i] < nums[k] < nums[j] with i < j < k.
Example 2 — Valid Pattern
$
Input:
nums = [3,1,4,2]
›
Output:
true
💡 Note:
Pattern found at indices (1,2,3): nums[1]=1, nums[2]=4, nums[3]=2, and 1 < 2 < 4 forms a 132 pattern.
Example 3 — Decreasing Array
$
Input:
nums = [-1,3,2,0]
›
Output:
true
💡 Note:
Pattern found at indices (0,1,2): nums[0]=-1, nums[1]=3, nums[2]=2, and -1 < 2 < 3 forms a 132 pattern.
Constraints
- n == nums.length
- 1 ≤ n ≤ 2 × 104
- -109 ≤ nums[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array with potential 132 pattern
2
Pattern Search
Look for three indices i < j < k with nums[i] < nums[k] < nums[j]
3
Result
Return true if pattern exists, false otherwise
Key Takeaway
🎯 Key Insight: Use a monotonic stack scanning backwards to efficiently track the largest valid middle element
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code