Maximum Value of an Ordered Triplet I - Problem

You're given a 0-indexed integer array nums and need to find the maximum value among all possible ordered triplets.

An ordered triplet consists of three indices (i, j, k) where i < j < k. The value of such a triplet is calculated as:

(nums[i] - nums[j]) * nums[k]

Goal: Return the maximum value over all valid triplets. If all triplets have negative values, return 0.

Example: For array [12, 6, 1, 2, 7], the triplet (0, 2, 4) gives us (12 - 1) * 7 = 77, which might be our maximum!

Input & Output

example_1.py โ€” Basic Positive Result
$ Input: [12, 6, 1, 2, 7]
โ€บ Output: 77
๐Ÿ’ก Note: The optimal triplet is (i=0, j=2, k=4): (12 - 1) ร— 7 = 11 ร— 7 = 77. This gives the maximum value among all possible ordered triplets.
example_2.py โ€” All Negative Values
$ Input: [1, 10, 3, 4, 19]
โ€บ Output: 133
๐Ÿ’ก Note: The optimal triplet is (i=4, j=1, k=4) - wait, that's invalid since i < j < k must hold. Actually, it's (i=0, j=1, k=4): (1 - 10) ร— 19 = -9 ร— 19 = -171, which is negative. Let's try (i=1, j=2, k=4): (10 - 3) ร— 19 = 7 ร— 19 = 133.
example_3.py โ€” Minimum Length Array
$ Input: [2, 3, 1]
โ€บ Output: 0
๐Ÿ’ก Note: Only one possible triplet: (i=0, j=1, k=2) gives us (2 - 3) ร— 1 = -1 ร— 1 = -1, which is negative. Since all triplet values are negative, we return 0.

Visualization

Tap to expand
๐Ÿ“ˆ Stock Trading Strategy: Buy โ†’ Sell โ†’ MultiplyBUY$12i=0Highest buy priceSELL$1j=2Profit: $12-$1 = $11MULTIPLYร—7k=4Final: $11 ร— 7 = $77๐Ÿ’ก Optimization Insight:โ€ข Track max_i: highest stock price seen so far (best buy opportunity)โ€ข Track max_diff: best profit potential (max_i - current_sell_price)โ€ข For each multiplier: calculate max_diff ร— current_multiplierโ€ข Update tracking variables for future positionsResult: O(n) time, O(1) space - optimal solution! ๐ŸŽฏ
Understanding the Visualization
1
Track Best Buy Price
As you move through time, remember the highest price you could have bought at (max_i)
2
Track Best Profit Potential
For each selling opportunity, calculate potential profit and remember the best (max_diff)
3
Apply Market Multiplier
When you reach each multiplier day, calculate final return using best profit ร— multiplier
Key Takeaway
๐ŸŽฏ Key Insight: By maintaining running maximums of the best buy price and best profit potential, we can find the optimal triplet in a single pass through the array, achieving O(n) time complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array, constant work per element

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables to track running maximums

n
2n
โœ“ Linear Space

Constraints

  • 3 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 106
  • All array elements are positive integers
  • Array indices must satisfy i < j < k for valid triplets
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
28.4K Views
Medium Frequency
~15 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen