Detect 132 Pattern
You are given an integer array nums of length n.
A 132 pattern is a triplet of indices (i, j, k) such that:
- 0 <= i < j < k < n
- nums[i] < nums[k] < nums[j]
In other words, you need to find three elements in order where:
- the first element is the smallest,
- the second element is the largest,
- the third element lies strictly between them.
Return true if there exists at least one such triplet in the array. Otherwise, return false.
Example 1:
Input: nums = [3, 5, 0, 3, 4]
Output: true
Explanation:
One valid 132 pattern is (3, 5, 4) using indices (0, 1, 4).
- 0 < 1 < 4
- nums[0] = 3
- nums[4] = 4
- nums[1] = 5
So, 3 < 4 < 5, which satisfies the 132 pattern.
Example 2:
Input: nums = [1, 2, 3, 4]
Output: false
Explanation:
The array is strictly increasing, so no triplet can satisfy nums[i] < nums[k] < nums[j] with i < j < k.
Now Your Turn!
Pick the correct output for the given inputInput: nums = [6, 12, 3, 4, 10]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- n == nums.length
- 1 <= n <= 2 * 105
- -109 <= nums[i] <= 109