Problem

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

Examples

Example 1:

1
2
3
4
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:

1
2
3
4
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:

1
2
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 <= 100
  • 1 <= nums[i] <= 106

Solution

Method 1 - Naive Iteration

To solve the problem efficiently, the objective is to calculate the maximum value of (nums[i] - nums[j]) * nums[k] for all triplets that satisfy the condition i < j < k. If all such triplet values are negative, the result should be 0.

Approach

  1. Iterate over the array with three nested loops: the outer loop for i, the middle loop for j, and the inner loop for k. Ensure the conditions i < j < k are satisfied.
  2. For each valid triplet (i, j, k), compute the value: (nums[i] - nums[j]) * nums[k].
  3. Keep track of the maximum value encountered (ans).
  4. If the maximum value (ans) is negative after evaluating all triplets, return 0; otherwise, return ans.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
    public long maximumTripletValue(int[] nums) {
        long ans = Long.MIN_VALUE;  // Use `long` since the return type is `long`
        int n = nums.length;       // Store the length of the array

        // Iterate over triplets respecting i < j < k
        for (int i = 0; i < n - 2; i++) {
            for (int j = i + 1; j < n - 1; j++) {
                for (int k = j + 1; k < n; k++) {
                    // Calculate the value of the triplet
                    long val = (long) (nums[i] - nums[j]) * nums[k];
                    // Update the maximum value
                    ans = Math.max(ans, val);
                }
            }
        }

        // Return the result or 0 if the maximum value is negative
        return Math.max(ans, 0);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
    def maximumTripletValue(self, nums: List[int]) -> int:
        ans: int = float('-inf')
        n: int = len(nums)
        
        # Iterate over triplets respecting i < j < k
        for i in range(n - 2):
            for j in range(i + 1, n - 1):
                for k in range(j + 1, n):
                    # Calculate the value of the triplet
                    val: int = (nums[i] - nums[j]) * nums[k]
                    # Update the maximum value
                    ans = max(ans, val)
        
        # Return the result or 0 if negative
        return max(ans, 0)

Complexity

  • ⏰ Time complexity: O(n^3) since it involves three nested loops over the array.
  • 🧺 Space complexity: O(1) as it requires a constant amount of extra space.