Skip to content

2874. Maximum Value of an Ordered Triplet II #1513

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

You must be logged in to vote

We need to find the maximum value of the triplet (i, j, k) such that i < j < k and the value is computed as (nums[i] - nums[j]) * nums[k]. If all such triplets have a negative value, we return 0.

Approach

  1. Precompute Maximum Values:

    • max_left: For each index j, store the maximum value of nums[i] where i < j.
    • max_right: For each index j, store the maximum value of nums[k] where k > j.
  2. Iterate Over Possible j Values:

    • For each valid j (from 1 to n-2), compute the value of the triplet using the precomputed max_left and max_right arrays. The value is calculated as (max_left[j] - nums[j]) * max_right[j].
  3. Track Maximum Value:

    • Keep track of the maximum value obtained from all valid tripl…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Apr 3, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Apr 3, 2025
Maintainer Author

Answer selected by basharul-siddike
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 medium Difficulty
2 participants