2873. Maximum Value of an Ordered Triplet I LeetCode Solution
In this guide, you will get 2873. Maximum Value of an Ordered Triplet I LeetCode Solution with the best time and space complexity. The solution to Maximum Value of an Ordered Triplet I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Value of an Ordered Triplet I solution in C++
- Maximum Value of an Ordered Triplet I solution in Java
- Maximum Value of an Ordered Triplet I solution in Python
- Additional Resources

Problem Statement of Maximum Value of an Ordered Triplet I
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 <= 100
1 <= nums[i] <= 106
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2873. Maximum Value of an Ordered Triplet I LeetCode Solution in C++
class Solution {
public:
long long maximumTripletValue(vector<int>& nums) {
long ans = 0;
int maxDiff = 0; // max(nums[i] - nums[j])
int maxNum = 0; // max(nums[i])
for (const int num : nums) {
ans = max(ans, static_cast<long>(maxDiff) * num); // num := nums[k]
maxDiff = max(maxDiff, maxNum - num); // num := nums[j]
maxNum = max(maxNum, num); // num := nums[i]
}
return ans;
}
};
/* code provided by PROGIEZ */
2873. Maximum Value of an Ordered Triplet I LeetCode Solution in Java
class Solution {
public long maximumTripletValue(int[] nums) {
long ans = 0;
int maxDiff = 0; // max(nums[i] - nums[j])
int maxNum = 0; // max(nums[i])
for (final int num : nums) {
ans = Math.max(ans, (long) maxDiff * num); // num := nums[k]
maxDiff = Math.max(maxDiff, maxNum - num); // num := nums[j]
maxNum = Math.max(maxNum, num); // num := nums[i]
}
return ans;
}
}
// code provided by PROGIEZ
2873. Maximum Value of an Ordered Triplet I LeetCode Solution in Python
class Solution:
def maximumTripletValue(self, nums: list[int]) -> int:
ans = 0
maxDiff = 0 # max(nums[i] - nums[j])
maxNum = 0 # max(nums[i])
for num in nums:
ans = max(ans, maxDiff * num) # num := nums[k]
maxDiff = max(maxDiff, maxNum - num) # num := nums[j]
maxNum = max(maxNum, num) # num := nums[i]
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.