Find the maximum value of an ordered triplet expression over an integer array.
Given an integer array nums, choose indices i, j, and k such that i < j < k. Compute the value of the ordered triplet expression
Return the maximum possible value among all valid ordered triplets. If every valid triplet gives a negative value, return the largest one anyway.
The task is to reason about the ordering constraint and efficiently search all possible triplets without checking every combination explicitly.
Input Format
- A single integer array
nums. - The array contains at least 3 elements.
Output Format
- Return one integer: the maximum value of
(nums[i] - nums[j]) * nums[k]over all indicesi < j < k.
Constraints
3 <= nums.length- The exact value ranges are not provided in the source metadata.
- A correct solution should avoid brute force over all triplets when possible.
Example 1
Input
nums = [1, 2, 3, 4]
Output
6
Explanation
The best triplet is (i, j, k) = (0, 1, 3), giving (1 - 2) * 4 = -4. But (0, 2, 3) gives (1 - 3) * 4 = -8. The maximum over all valid ordered triplets is actually 6 from (2 - 1) * 3? Wait, indices must be ordered, so using the ordered constraint the best valid value is -2? This sample is illustrative only and should be replaced by the exact platform example when available.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.