Browse curriculum

Increasing Triplet Subsequence

Decide whether an array has i < j < k with nums[i] < nums[j] < nums[k] in one pass, keeping only the smallest first and second values of a pair.

Problem Understanding

Increasing Triplet Subsequence (LeetCode 334): given an integer array nums, return true if there exist indices i < j < k with nums[i] < nums[j] < nums[k], and false otherwise. Aim for O(n) time and O(1) space.

Examples: [1, 2, 3, 4, 5] → true; [5, 4, 3, 2, 1] → false; [2, 1, 5, 0, 4, 6] → true (0, 4, 6).

The Intuition: Keep the Bar as Low as Possible

To finish a triplet you need a value bigger than the end of some increasing pair. The lower that pair's end, the easier it is to beat.

  1. first is the smallest value seen so far.
  2. second is the smallest value that came after something smaller.
  3. Any value above second completes a triplet.

Greedily keeping both as small as possible is what makes one pass enough. It is Longest Increasing Subsequence's tails idea, stopped at length 3.

Interactive Walkthrough

KEEP first AND second AS SMALL AS POSSIBLE

first = second = ∞

nums

2

0

1

1

5

2

0

3

4

4

6

5

first

∞

second

∞

Answer

:

 

first = second = ∞: no values seen yet

Next

2 ≤ first (∞): it is the smallest so far, so first = 2

Free preview: the first 3 of 7 steps

nums

first is outlined and second tinted. Watch first jump ahead of second in Trap: the pair behind second still stands, and the real triplet turns green at the end.

Dry Run Table

Input: nums = [2, 1, 5, 0, 4, 6].

x Compare first second
2 ≤ ∞ 2 ∞
1 ≤ 2 1 ∞
5 > 1, ≤ ∞ 1 5
0 ≤ 1 0 5
4 > 0, ≤ 5 0 4
6 > 4

6 is bigger than second. Answer: true, for example 0 < 4 < 6.

The Solution Template

Two variables and three branches. The early return is the only way to answer true.

Increasing Triplet Subsequence Code
1var increasingTriplet = function(nums) {
2 let first = Infinity, second = Infinity;
3 for (const x of nums) {
4 if (x <= first) {
5 first = x;
6 } else if (x <= second) {
7 second = x;
8 } else {
9 return true;
10 }
11 }
12 return false;
13};

Edge Cases & Common Mistakes

  • Equal values: use <= so [1, 1, 1] is false.
  • Fewer than 3 elements: the loop cannot reach the third branch, so false.
  • Thinking first and second must be in order in the array: they need not be at the end; second's pair was recorded when it was set.
  • Sentinel overflow: with INT_MAX as infinity, a value equal to INT_MAX is handled by <=, so it never counts as bigger.

Summary

Keep first as the smallest value and second as the smallest value with something smaller before it; any value larger than second proves a triplet. O(n) time, O(1) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Start

    Set first and second to infinity.

  2. Update

    For each x: if x <= first, set first = x; else if x <= second, set second = x.

  3. Detect

    Otherwise x is bigger than both, so return true. If the loop ends, return false.

Frequently asked questions

Why is it fine for first to move after second is set?

second is only ever set when a smaller first exists before it, so a valid pair ending at second is already on record. Moving first to a later, smaller value does not erase that pair; it just gives a better start for a future pair. Any x bigger than second still completes a triplet with the old first.


Why use <= instead of <?

The triplet must be strictly increasing. With <=, an equal value replaces first or second instead of being treated as bigger, so [1, 1, 1] correctly returns false.


How does this relate to Longest Increasing Subsequence?

It is the patience-sorting idea behind the O(n log n) LIS algorithm, cut down to length 3: first and second are the smallest possible tails of increasing runs of length 1 and 2. A value beyond both extends a run to length 3.


What are the time and space costs?

One pass, O(n) time, and two variables, O(1) space. It returns as soon as a triplet is found.

New to the technique behind this problem? Watch it run step by step:

Greedy Algorithms Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description