LeetCode : [334] Increasing Triplet Subsequence

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.


  • 1 <= nums.length <= 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 -1 : Brute Force

Idea is to check for all combinations which holds true condition that is nums[i] < nums[j] < nums[k] for i < j < k .

Time Complexity : O(n³)

Space Complexity : O(1)

Approach - 2 : Using Stack

We can observe that we just need to check whether an element has smaller value in left side and greater value in right side , so we can find left smaller and right greater in O(n) using stack .

Since we really need only one combination that holds this condition true , so we are storing all left smaller and then check for right greater if we found then return true .

Time Complexity : O(n) , for scanning two times and each time every element will be accessed at most two times

Space Complexity : O(n) , for stack and left array

Approach - 3 : Storing smaller and next smaller

If we have two element a and b where a < b and now if we found c as b < c then we will return true , now if we found a less than previous a then we will update a .

Time Complexity : O(n)

Space Complexity : O(1)

Thanks …



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vikas Shekhawat

Vikas Shekhawat


Sole Master | Software engineer and a happy 😊 person | Former Irresponsible Boy !!!