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
132 Pattern: Find subsequence where nums[i] < nums[k] < nums[j]Example: [3, 1, 4, 2]3142i=0i=1i=2i=31 (position i=1)2 (position k=3)4 (position j=2)Pattern Found: 1 < 2 < 4 with indices 1 < 3 < 2? NOCorrect: 1 < 2 < 4 with indices 1 < 2 < 3 ✓Output: true
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
Asked in
Amazon 15 Google 12 Microsoft 8 Facebook 6
89.4K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen