AlgoMaster Logo

Increasing Triplet Subsequence

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Brute Force Approach

Intuition:

The simplest approach to find an increasing triplet subsequence is to use three nested loops to check all possible triplets in the array.

This is a naive solution but helps to understand the underlying concept.

Code:

2. Linear Scan using Two Variables

Intuition:

The goal is to find three increasing numbers a, b, c such that a < b < c. We can achieve this in linear time with two variables:

  • first: This will keep track of the smallest number found so far.
  • second: This will keep track of a number larger than first.

As we scan through the array, the goal is to gradually refine these candidates. If at any point we encounter a number greater than both first and second, it becomes our third, and the triplet exists.

Code:

Example Walkthrough:

0
2
1
1
2
5
3
0
4
4
5
6
first = ∞, second = ∞
Step 1 / 7