AlgoInsight

DSA problem

Increasing Triplet Subsequence

MediumArraysPattern: Greedytop300AmazonGoogleFacebookMicrosoftBloomberg

Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exist, return false.

This is a classic greedy problem. A brute-force approach checks every triple in O(n^3) time, which is far too slow for large inputs. The optimal solution scans the array once while tracking two values: the smallest element seen so far (first) and the smallest element that has a strictly smaller predecessor (second).

If at any point we encounter a number strictly greater than second, we have found a valid increasing triplet and can return true immediately.

Examples

Example 1
Input: nums = [1,2,3,4,5]
Output: true
Explanation: Any triplet where i < j < k is valid, for example (0, 1, 2).
Example 2
Input: nums = [5,4,3,2,1]
Output: false
Explanation: The array is strictly decreasing, so no increasing triplet exists.
Example 3
Input: nums = [2,1,5,0,4,6]
Output: true
Explanation: The triplet (3, 4, 5) is valid because nums[3] = 0 < nums[4] = 4 < nums[5] = 6.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

arraygreedytwo-pointers

Browse all DSA problems