132 Pattern
Problem Statement
You are given an integer array nums.
A 132 pattern is three positions i < j < k where
nums[i] < nums[k] < nums[j].
The name comes from the shape: the first value is the smallest ("1"), the middle one is the largest ("3"), and the last one is in between ("2"). The three positions don't have to be next to each other.
Return true if the array contains a 132 pattern, otherwise false.
An integer array nums.
true or false.
The values only go up, so no later value is ever smaller than an earlier one.
1, 4, 2 is a 132 pattern: 1 < 2 < 4.
There are three: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
- • 1 ≤ n ≤ 2·10^5
- • −10^9 ≤ nums[i] ≤ 10^9
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Three nested loops over i < j < k
For each j, use the smallest value before it as the "1", then scan k > j
Scan right to left; values popped by a bigger value become the "2"; keep the largest
Try every i < j < k and check nums[i] < nums[k] < nums[j]. Return true on the first match.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Monotonic Stack translate into production architectures and low-level memory mechanics.