# Solving Leetcode Interviews in Seconds with AI: Maximum Value of an Ordered Triplet I


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "2873" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> 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  

	# Explanation
	Here's the breakdown of the solution:

*   **Iterate and Calculate:** The solution iterates through all possible triplets of indices (i, j, k) where i < j < k. For each triplet, it calculates the value (nums[i] - nums[j]) * nums[k].
*   **Maximize:** It keeps track of the maximum value encountered so far and updates it whenever a larger triplet value is found.
*   **Handle Negative Values:** If the maximum value remains negative after checking all triplets (meaning all triplets have negative values), the solution returns 0.

*   **Runtime Complexity:** O(n^3), **Storage Complexity:** O(1)

	
	# Code
	```python
	def max_value_of_triplet(nums):
    """
    Calculates the maximum value over all triplets of indices (i, j, k) such that i < j < k.

    Args:
        nums: A list of integers.

    Returns:
        The maximum value over all triplets, or 0 if all triplets have a negative value.
    """

    max_val = float('-inf')
    n = len(nums)

    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            for k in range(j + 1, n):
                val = (nums[i] - nums[j]) * nums[k]
                max_val = max(max_val, val)

    if max_val < 0:
        return 0
    else:
        return max_val
	```
			
