2Brute2Best
🔥0

132 Pattern

Medium·LeetCode #456 Pro OnlyTricky / Edge Case
LeetCode
01

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.

Input

An integer array nums.

Output

true or false.

Example 1
Input: nums = [1, 2, 3, 4]
Output: false

The values only go up, so no later value is ever smaller than an earlier one.

Example 2
Input: nums = [3, 1, 4, 2]
Output: true

1, 4, 2 is a 132 pattern: 1 < 2 < 4.

Example 3
Input: nums = [-1, 3, 2, 0]
Output: true

There are three: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].

Constraints
  • • 1 ≤ n ≤ 2·10^5
  • • −10^9 ≤ nums[i] ≤ 10^9
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“i < j < k with nums[i] < nums[k] < nums[j]”
→ Three positions with an order and a value condition. Fix the hardest part (the "3, 2" pair) with a stack, the easy part (the "1") with a single comparison.
“nums[k] < nums[j], j before k”
→ The "2" needs a bigger value on its left — a previous-greater relation, which a monotonic stack finds when it pops.
“return true / false”
→ We only need one pattern, so we keep only the most useful candidate (the largest "2").
03

Approach Complexity Ladder

Brute Force — try every tripleO(N³)O(1)

Three nested loops over i < j < k

Better — prefix minimum as the "1"O(N²)O(1)

For each j, use the smallest value before it as the "1", then scan k > j

Optimal — right-to-left stack with the best "2"O(N)O(N)

Scan right to left; values popped by a bigger value become the "2"; keep the largest

Brute Force Intuition

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:

"How many consecutive previous items are ≤ me?" Popped items fold their span into the new one.
Remember this · Monotonic Stack
Trigger
Next/previous greater or smaller element, spans, histogram areas, "for each element, how far does it dominate?"
Think
When a new element arrives, which older elements can never be the answer again?
Key question
Is my stack increasing or decreasing, and what do I record when I pop?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Monotonic Stack translate into production architectures and low-level memory mechanics.