2Brute2Best
🔥0

Increasing Triplet Subsequence

Medium·LeetCode #334 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given a list of numbers nums.

Decide if there are three positions i < j < k, in that order, where nums[i] < nums[j] < nums[k]. The three numbers do not need to be next to each other.

Follow-up: can you do it in one pass with only a few variables?

Input

An integer array nums.

Output

true if such a triple exists, otherwise false.

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

Any three positions in order work, for example 1, 2, 3.

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

The numbers only go down, so no triple exists.

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

The numbers 1, 4, 6 are at positions 1, 4, 5 and increase. The numbers 0, 4, 6 at positions 3, 4, 5 also work.

Constraints
  • • 1 ≤ nums.length ≤ 5 · 10^5
  • • −2^31 ≤ nums[i] ≤ 2^31 − 1
02

Pattern Recognition Clues

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

“triple of indices i < j < k”
→ Only the order of the positions matters. The three numbers do not need to touch, so this is a subsequence of length 3.
“nums[i] < nums[j] < nums[k]”
→ A strictly increasing run of length 3. Equal numbers fail the check.
“return true or false”
→ We only need to know if a length-3 chain exists, so we can stop as soon as one is found.
03

Approach Complexity Ladder

Brute Force — try every tripleO(N^3)O(1)

Check every triple of positions i < j < k

Better — prefix minimum and suffix maximumO(N)O(N)

For each middle item, compare it with the smallest value on its left and the largest on its right

Optimal — two variables: first and secondO(N)O(1)

Track the smallest value so far, and the smallest middle candidate; a bigger value finishes a triple

Brute Force Intuition

Use three nested loops to pick positions i < j < k, and test nums[i] < nums[j] < nums[k]. If any triple passes, the answer is true.

Recognition rule for this sub-pattern:

O(n²): try every earlier j. O(n log n): binary search the tails array.
Remember this · DP — Longest Increasing Subsequence
Trigger
Longest chain where each item must be bigger/fit inside the previous one (numbers, pairs, envelopes, words).
Think
Does each element extend the best chain ending at some smaller earlier element?
Key question
Sort first? Then what does "increasing" mean for these items?
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 DP — Longest Increasing Subsequence translate into production architectures and low-level memory mechanics.