Maximum Value of an Ordered Triplet I

You are given a 0-indexed integer array `nums`. Return the maximum value of `(nums[i] - nums[j]) * nums[k]` over all index triplets `i < j < k`. If every triplet has a negative value, return 0. The answer can exceed 32 bits.

30:00

Maximum Value of an Ordered Triplet I
easy
Free written solution
Topics
Companies

You are given a 0-indexed integer array nums. Return the maximum value of (nums[i] - nums[j]) * nums[k] over all index triplets i < j < k. If every triplet has a negative value, return 0. The answer can exceed 32 bits.

Example 1:
Input: {"nums":[12,6,1,2,7]}
Output: 77
Constraints:
  • 3≤nums.length≤1003 \leq nums.\text{length} \leq 100

  • 1≤nums[i]≤1061 \leq nums[i] \leq 10^6

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[12,6,1,2,7]}
1. try it as k
2. try it as j
3. keep it as i

12

0

6

1

1

2

2

3

7

4

Read each value as k first, then as j, then as i. Carry the best nums[i] seen so far and the best nums[i] - nums[j] seen so far.

max_i (best left end)

0

nothing yet

max_diff = nums[i] - nums[j]

0

no drop yet

ans = max_diff × nums[k]

0

0 so far

Variables
VariableValue
max_i0
max_diff0
ans0
DepthFunction Call
Stack empty
0/16