Skip to main content

Increasing Triplet Subsequence - Solution & Explanation

MediumArrayGreedy10 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

Problem Statement

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 exists, return false.

 

Example 1:

Input: nums = [1,2,3,4,5]
Output: true
Explanation: Any triplet where i < j < k is valid.

Example 2:

Input: nums = [5,4,3,2,1]
Output: false
Explanation: No 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:

  • 1 <= nums.length <= 5 * 105
  • -231 <= nums[i] <= 231 - 1

 

Follow up: Could you implement a solution that runs in O(n) time complexity and O(1) space complexity?

Approach Overview

Problem Overview: You receive an integer array nums. The task is to determine whether there exist three indices i < j < k such that nums[i] < nums[j] < nums[k]. The elements do not need to be consecutive, only ordered by index. The goal is to detect the existence of such a triplet efficiently.

Approach 1: DP-like Subsequence Tracking with Array (O(n²) time, O(n) space)

This method treats the problem as a variation of the Longest Increasing Subsequence. Create a dp array where dp[i] stores the length of the longest increasing subsequence ending at index i. Iterate through the array, and for each index i, scan all previous indices j < i. If nums[j] < nums[i], update dp[i] = max(dp[i], dp[j] + 1). The moment any value in dp reaches 3, an increasing triplet exists and you can return true.

This approach is straightforward and closely mirrors classic LIS logic. It explicitly tracks subsequence length and is easy to reason about during interviews when explaining subsequence problems. The tradeoff is performance: nested iteration leads to O(n²) time, which becomes slow for large inputs.

Approach 2: Greedy Using Two Variables (O(n) time, O(1) space)

The optimal solution uses a greedy observation: you only need to track the smallest possible first and second values of a potential triplet. Maintain two variables: first and second. Initialize both to a large value. Iterate through the array once. If the current number is smaller than or equal to first, update first. If it is greater than first but smaller than or equal to second, update second. If a number is greater than both, you found first < second < current, which confirms an increasing triplet.

The key insight: always keep the smallest possible candidates for the first two positions so a third larger value can complete the sequence. Each element is processed once, producing O(n) time with only constant extra memory. This pattern is a classic greedy optimization applied to an array traversal.

Recommended for interviews: The greedy two-variable approach is what most interviewers expect. It demonstrates that you recognize the subsequence constraint and can reduce the problem to maintaining minimal candidates during a single pass. Mentioning the DP/LIS-style approach first shows you understand the brute-force progression, but implementing the O(n) greedy solution shows strong algorithmic intuition.

Approach 1: Greedy Approach using Two Variables

This approach leverages two variables to track the smallest and the second smallest elements found so far in the array. As you traverse through the array, you update these two variables. If you find an element greater than the second smallest, then a triplet exists, and you return true.

This C solution iterates over the array, updating two variables, first and second, that keep track of the smallest and second smallest values. If a current element is greater than second, it confirms the existence of an increasing triplet.

Code

C

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the input list, as we only traverse the list once.
Space Complexity: O(1), since we are using only a constant amount of extra space.

Try this approach in the editor →

Approach 2: Using an Array for DP-like Solution

This approach uses an auxiliary array to keep track of the minimum elements on the left and the maximum elements on the right for each position in the array. The main idea is to check if there's any element that can serve as the middle element between some smaller and a larger element.

This solution creates two arrays, leftMin and rightMax, to store the minimum values to the left and maximum values to the right of each element. It then checks for a valid increasing triplet by ensuring there's a middle element greater than the minimum from the left and less than the maximum from the right.

Code

C

Java

Python

Complexity

Time Complexity: O(n), due to traversals of size n.
Space Complexity: O(n), since it uses extra space for two arrays.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Greedy Approach using Two Variables

Time Complexity: O(n), where n is the length of the input list, as we only traverse the list once.
Space Complexity: O(1), since we are using only a constant amount of extra space.

Using an Array for DP-like Solution

Time Complexity: O(n), due to traversals of size n.
Space Complexity: O(n), since it uses extra space for two arrays.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
DP-like LIS Tracking with ArrayO(n²)O(n)When explaining the problem from a subsequence/LIS perspective or when building intuition before optimizing
Greedy Using Two VariablesO(n)O(1)Best general solution for interviews and production due to linear time and constant memory

Video Solution

Increasing Triplet Subsequence | Live Coding with Explanation | Leetcode #334Algorithms Made Easy29,868 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Increasing Triplet Subsequence easy or hard?
Increasing Triplet Subsequence is classified as a Medium difficulty problem. The brute-force or DP-style reasoning is straightforward, but recognizing the greedy optimization that reduces the problem to two tracked values requires deeper algorithmic insight.
Increasing Triplet Subsequence Python/Java solution
Python and Java implementations typically follow the greedy two-variable pattern. Initialize two variables to large values, iterate through the array, update the first or second candidate accordingly, and return true when a third larger value appears. The implementation is short and runs in O(n) time.
How to solve Increasing Triplet Subsequence in O(n)?
Maintain two variables: the smallest value (first) and the second smallest value (second). Traverse the array and update first if the current number is smaller, update second if it fits between first and second, and return true if a number exceeds both. This single-pass greedy strategy detects a valid triplet in linear time.
What is the best approach for Increasing Triplet Subsequence?
The optimal approach is a greedy algorithm that tracks two variables representing the smallest and second-smallest values seen so far. While iterating through the array, update these values and return true when a number larger than both appears. This solution runs in O(n) time and O(1) space, making it the most efficient method.
Is Increasing Triplet Subsequence asked at Google/Amazon/Meta?
Increasing Triplet Subsequence frequently appears in interviews at large tech companies including Google, Amazon, and Meta. The question tests understanding of greedy reasoning, subsequences, and the ability to optimize from a quadratic LIS-style approach to a linear-time solution.
What data structure is used in Increasing Triplet Subsequence?
The optimal solution does not require complex data structures. It relies on simple variable tracking during a linear scan of the array. Alternative approaches may use a dynamic programming array to track subsequence lengths.
What is the time complexity of Increasing Triplet Subsequence?
The optimal greedy solution runs in O(n) time because the array is scanned once. It uses constant extra memory, so the space complexity is O(1). A simpler dynamic programming approach based on longest increasing subsequence takes O(n²) time and O(n) space.

Ready to solve this problem?

Practice Increasing Triplet Subsequence with our built-in code editor and test cases.

Practice on FleetCode