Skip to content

2873. Maximum Value of an Ordered Triplet I #1509

Answered by mah-shamim
topugit asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the maximum value of all possible ordered triplets (i, j, k) such that i < j < k in a given array. The value of each triplet is calculated as (nums[i] - nums[j]) * nums[k]. If all such triplets have a negative value, we return 0.

Approach

The key insight to optimize the solution is to recognize that for each pair (i, j), the maximum possible value of the triplet (i, j, k) can be determined by the maximum value of nums[k] for k > j. This allows us to precompute the maximum value to the right of each index j, which reduces the problem from O(n^3) time complexity to O(n^2).

  1. Precompute Maximum Values: Create an array max_right where each element at index j is the maximum valu…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Apr 2, 2025
Collaborator Author

@mah-shamim
Comment options

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants