Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Increasing Triplet Subsequence in Python
An increasing triplet subsequence is a sequence of three numbers from an array where each number is smaller than the next one, and they appear in the same order as in the original array (but not necessarily consecutive).
The problem asks us to determine if such a triplet exists in an unsorted array.
Problem Definition
Given an array, return True if there exists indices i, j, k such that:
arr[i] < arr[j] < arr[k]0 ≤ i < j < k ≤ n-1
Otherwise, return False.
Algorithm Approach
We use a greedy approach with two variables to track potential candidates:
-
small− tracks the smallest element seen so far -
big− tracks the second smallest element that comes aftersmall
For each element:
- If it's smaller than or equal to
small, updatesmall - Else if it's smaller than or equal to
big, updatebig - Else we found our third element − return
True
Implementation
def increasing_triplet(nums):
small = float('inf')
big = float('inf')
for num in nums:
if num <= small:
small = num
elif num <= big:
big = num
else:
return True
return False
# Test the function
test_array = [5, 3, 8, 2, 7, 9, 4]
result = increasing_triplet(test_array)
print(f"Array: {test_array}")
print(f"Has increasing triplet: {result}")
Array: [5, 3, 8, 2, 7, 9, 4] Has increasing triplet: True
Step-by-Step Trace
Let's trace through the array [5, 3, 8, 2, 7, 9, 4]:
def increasing_triplet_with_trace(nums):
small = float('inf')
big = float('inf')
print(f"Initial: small={small}, big={big}")
for i, num in enumerate(nums):
if num <= small:
small = num
print(f"Step {i+1}: num={num}, updated small={small}, big={big}")
elif num <= big:
big = num
print(f"Step {i+1}: num={num}, small={small}, updated big={big}")
else:
print(f"Step {i+1}: num={num}, Found triplet! ({small}, {big}, {num})")
return True
print("No increasing triplet found")
return False
# Trace the example
increasing_triplet_with_trace([5, 3, 8, 2, 7, 9, 4])
Initial: small=inf, big=inf Step 1: num=5, updated small=5, big=inf Step 2: num=3, updated small=3, big=inf Step 3: num=8, small=3, updated big=8 Step 4: num=2, updated small=2, big=8 Step 5: num=7, small=2, updated big=7 Step 6: num=9, Found triplet! (2, 7, 9)
Testing Different Cases
def test_cases():
test_arrays = [
[1, 2, 3], # Simple increasing
[3, 2, 1], # Decreasing
[5, 3, 8, 2, 7, 9], # Mixed with triplet
[1, 1, 1, 1], # All same
[1, 5, 3, 6, 4] # Has triplet: 1, 3, 4
]
for arr in test_arrays:
result = increasing_triplet(arr)
print(f"Array: {arr} ? {result}")
test_cases()
Array: [1, 2, 3] ? True Array: [3, 2, 1] ? False Array: [5, 3, 8, 2, 7, 9] ? True Array: [1, 1, 1, 1] ? False Array: [1, 5, 3, 6, 4] ? True
Time and Space Complexity
- Time Complexity: O(n) − single pass through the array
- Space Complexity: O(1) − only two variables used
Conclusion
The greedy approach efficiently finds increasing triplets by maintaining two candidates. This solution works in linear time and constant space, making it optimal for the problem.
