Maximum Value of an Ordered Triplet II
MedPrompt
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:
Input: nums = [12,6,1,2,7]
Output: 77
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77. Example 2:
Input: nums = [1,10,3,4,19]
Output: 133
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.Example 3:
Input: nums = [1,2,3]
Output: 0
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
Constraints:
3 <= nums.length <= 1051 <= nums[i] <= 106
Approaches
3 approaches with complexity analysis and trade-offs.
The most straightforward approach is to use brute force. We can generate every possible ordered triplet of indices (i, j, k) where i < j < k, calculate the value (nums[i] - nums[j]) * nums[k] for each triplet, and keep track of the maximum value found. We initialize our maximum value to 0, as the problem states to return 0 if all triplet values are negative.
Algorithm
- Initialize a variable
maxValueto 0, using alongdata type to prevent potential overflow. - Use three nested loops to iterate through all possible combinations of indices
(i, j, k)such thati < j < k.- The outer loop for
iruns from0ton-3. - The middle loop for
jruns fromi+1ton-2. - The inner loop for
kruns fromj+1ton-1.
- The outer loop for
- Inside the innermost loop, calculate the value of the triplet:
currentValue = (long)(nums[i] - nums[j]) * nums[k]. - Compare
currentValuewithmaxValueand updatemaxValueifcurrentValueis greater:maxValue = Math.max(maxValue, currentValue). - After the loops complete,
maxValuewill hold the maximum possible value. Since the problem asks to return 0 for negative results, and ourmaxValueis initialized to 0 and only updated with larger values, this condition is naturally met. - Return
maxValue.
Walkthrough
This method involves three nested loops. The first loop iterates through the index i from the beginning of the array. The second loop iterates through j starting from i + 1, and the third loop iterates through k starting from j + 1. This ensures that we only consider valid ordered triplets (i < j < k). For each triplet, we compute its value and update our overall maximum. Using a long for the result is crucial to avoid integer overflow, as the product can exceed the capacity of a standard 32-bit integer.
class Solution { public long maximumValueSum(int[] nums) { int n = nums.length; long maxValue = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { long currentValue = (long)(nums[i] - nums[j]) * nums[k]; if (currentValue > maxValue) { maxValue = currentValue; } } } } return maxValue; }}Complexity
Time
O(N^3) - Where N is the length of the `nums` array. The three nested loops lead to a cubic time complexity, which is too slow for N up to 10^5.
Space
O(1) - Constant extra space is used, as we only need a few variables to store the loop indices and the maximum value.
Trade-offs
Pros
Simple to understand and implement.
Correct for small input sizes.
Cons
Extremely inefficient for large inputs.
Will result in a 'Time Limit Exceeded' (TLE) error on most platforms for the given constraints.
Solutions
Solution
class Solution {public long maximumTripletValue(int[] nums) { long max, maxDiff, ans; max = 0; maxDiff = 0; ans = 0; for (int num : nums) { ans = Math.max(ans, num * maxDiff); max = Math.max(max, num); maxDiff = Math.max(maxDiff, max - num); } return ans; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.