Mid/SeniorArray

Maximum Value of an Ordered Triplet II

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].

Example 1
Inputnums = [12,6,1,2,7]
Output77
The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77, and there are no ordered triplets with a greater value.
Example 2
Inputnums = [1,10,3,4,19]
Output133
The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133, and there are no ordered triplets with a greater value.

Constraints

  • 3 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^6

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate