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
Asked in
Amazon 15 Google 12 Microsoft 8 Facebook 6
93.5K 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