Maximum Increasing Triplet Value - Problem
Given an integer array nums, return the maximum value of a triplet (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k].
The value of a triplet (i, j, k) is defined as nums[i] - nums[j] + nums[k].
If no such triplet exists, return 0.
Input & Output
Example 1 — Basic Increasing Triplet
$
Input:
nums = [2,1,3,4]
›
Output:
3
💡 Note:
The optimal triplet is at indices (0,2,3) with values (2,3,4). Since 2 < 3 < 4, the triplet value is 2 - 3 + 4 = 3.
Example 2 — Multiple Valid Triplets
$
Input:
nums = [1,2,3,4,5]
›
Output:
4
💡 Note:
Multiple triplets exist. The best is (0,1,4) with values (1,2,5), giving 1 - 2 + 5 = 4. Any triplet (i,j,k) with i < j < k works since array is strictly increasing.
Example 3 — No Valid Triplet
$
Input:
nums = [5,4,3,2,1]
›
Output:
0
💡 Note:
Array is decreasing, so no triplet (i,j,k) exists where nums[i] < nums[j] < nums[k]. Return 0.
Constraints
- 3 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code