Maximum Value of an Ordered Triplet II - Problem
You are given a 0-indexed integer array nums.
Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.
The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].
Input & Output
Example 1 — Basic Case
$
Input:
nums = [12,6,1,2,7]
›
Output:
77
💡 Note:
The best triplet is (0,2,4): (nums[0] - nums[2]) * nums[4] = (12 - 1) * 7 = 77.
Example 2 — All Negative Results
$
Input:
nums = [1,10,3,4,19]
›
Output:
133
💡 Note:
The best triplet is (1,2,4): (nums[1] - nums[2]) * nums[4] = (10 - 3) * 19 = 133.
Example 3 — Return Zero Case
$
Input:
nums = [1,2,3]
›
Output:
0
💡 Note:
The only triplet is (0,1,2): (1 - 2) * 3 = -3, which is negative, so we 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