Maximum Value of an Ordered Triplet I (LeetCode 2873): Solution and Explanation

Easy
Array

For indices i < j < k, a triplet is worth (nums[i] - nums[j]) * nums[k]. Return the largest value over all triplets, or 0 if every triplet is negative. For [12,6,1,2,7] the answer is 77. Products reach 10^12, so the result is a 64-bit integer.

Intuition

All values are positive, so for a fixed k the product is largest when the gap nums[i] - nums[j] is as large as possible among indices before k.

For a fixed j, the best i is just the largest value to the left of j. So the best gap is a running maximum of max_i - nums[j].

Each element plays three roles as the scan passes it: first it is tried as k, then as j, then it may become the new i. Doing the updates in that order keeps the indices strictly increasing.

Approach: One pass with a prefix maximum and a best gap

  1. Set ans = 0, max_i = 0 and max_diff = 0.
  2. For each value x, first update ans = max(ans, max_diff * x). Here x acts as k.
  3. Then update max_diff = max(max_diff, max_i - x). Here x acts as j.
  4. Then update max_i = max(max_i, x). Here x becomes a candidate i.
  5. Return ans, computed with 64-bit arithmetic.

Step-by-step example

nums = [12,6,1,2,7]
  1. x = 12: no gap yet, so ans stays 0. It becomes max_i = 12.
  2. x = 6: as k it gives 0. As j the gap is 12 - 6 = 6, so max_diff = 6.
  3. x = 1: as k it gives 6 × 1 = 6, so ans = 6. As j the gap is 12 - 1 = 11, so max_diff = 11.
  4. x = 2: as k it gives 11 × 2 = 22, so ans = 22. The gap 12 - 2 = 10 is smaller than 11.
  5. x = 7: as k it gives 11 × 7 = 77, so ans = 77. The answer is 77 from the triplet (0, 2, 4).

Code: Python, Java, C++

1class Solution:
2    def maximumTripletValue(self, nums: List[int]) -> int:
3        ans = 0
4        max_i = 0
5        max_diff = 0
6        for x in nums:
7            ans = max(ans, max_diff * x)
8            max_diff = max(max_diff, max_i - x)
9            max_i = max(max_i, x)
10        return ans
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each element is visited once and does three constant-time updates.

Space

O(1)

Only three running values are kept.

Other approaches

Brute force over all triplets (time O(n^3), space O(1))

Three nested loops try every i < j < k. With n at most 100 this is about 160,000 triplets, which is accepted for this version of the problem.

Prefix and suffix maxima (time O(n), space O(n))

Precompute the largest value to the left of each j and the largest value to the right of it, then take (leftMax[j] - nums[j]) * rightMax[j] for every middle index.

Common mistakes

  • Updating max_i before max_diff, or max_diff before ans. Then the same element could act as two indices of one triplet.
  • Overflowing 32-bit integers. A gap near 10^6 times a value near 10^6 is about 10^12, so use long, long long or int64.
  • Returning a negative number. Starting ans at 0 handles the rule that a negative best becomes 0.

Frequently asked questions

Why is it safe to start max_i and max_diff at 0?

Every value is at least 1, so the first real value replaces max_i = 0. A max_diff of 0 produces a product of 0, which never beats the starting answer of 0.

Do I need the minimum value anywhere?

No. The multiplier nums[k] is always positive, so a negative gap can never help. Only the largest gap matters.

What changes in Maximum Value of an Ordered Triplet II?

Only the limits grow to 10^5 elements. The brute force stops working, but this one-pass solution is already linear and works unchanged.