Increasing Triplet Subsequence - Problem

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.

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

Input & Output

Example 1 — Increasing Sequence
$ Input: nums = [1,2,3,4,5]
Output: true
💡 Note: Any triplet (i,j,k) with i < j < k satisfies the condition. For instance, (0,1,2) gives us 1 < 2 < 3.
Example 2 — No Valid Triplet
$ Input: nums = [5,4,3,2,1]
Output: false
💡 Note: No triplet satisfies the condition since the array is decreasing.
Example 3 — Mixed Values
$ Input: nums = [2,1,5,0,4,6]
Output: true
💡 Note: The triplet (1,4,5) gives indices with values 1 < 4 < 6.

Constraints

  • 1 ≤ nums.length ≤ 5 × 105
  • -231 ≤ nums[i] ≤ 231 - 1

Visualization

Tap to expand
Increasing Triplet Subsequence INPUT Integer Array nums[] 1 i=0 2 i=1 3 i=2 4 i=3 5 i=4 Goal: Find triplet (i,j,k) where i < j < k and nums[i] < nums[j] < nums[k] Two Variables: first = INF, second = INF Input: nums = [1,2,3,4,5] ALGORITHM STEPS 1 Initialize Variables first=INF, second=INF 2 Iterate Array For each num in nums[] 3 Update first/second Keep smallest values 4 Check Third Value If num > second: return true Execution Trace: num first second result 1 1 INF - 2 1 2 - 3 1 2 true 3 > second(2) --> Found triplet! FINAL RESULT Found Triplet: 1 first < 2 second < 3 third Indices: i=0, j=1, k=2 Values: 1 < 2 < 3 Output: true Status: OK Triplet exists! Key Insight: The greedy two-variable approach tracks the smallest and second smallest values seen so far. When we find a number greater than both, we have our increasing triplet subsequence. Time Complexity: O(n) | Space Complexity: O(1) - Only two variables needed! TutorialsPoint - Increasing Triplet Subsequence | Greedy Two Variables Approach
Asked in
Facebook 42 Google 38 Amazon 31 Microsoft 25
205.4K Views
High Frequency
~15 min Avg. Time
6.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen