You are given an integer array nums.
Choose three elements a, b, and c from nums at distinct indices such that the value of the expression a + b - c is maximized.
Return an integer denoting the maximum possible value of this expression.
Example 1:
Input: nums = [1,4,2,5]
Output: 8
Explanation:
We can choose a = 4, b = 5, and c = 1. The expression value is 4 + 5 - 1 = 8, which is the maximum possible.
Example 2:
Input: nums = [-2,0,5,-2,4]
Output: 11
Explanation:
We can choose a = 5, b = 4, and c = -2. The expression value is 5 + 4 - (-2) = 11, which is the maximum possible.
Constraints:
3 <= nums.length <= 100-100 <= nums[i] <= 100Problem Overview: You are given an array and need to maximize the value of an expression formed using three different elements. The indices must be distinct, and the goal is to choose the combination of numbers that produces the largest possible value.
Approach 1: Enumeration (Brute Force) (Time: O(n^3), Space: O(1))
The most direct strategy checks every valid triplet of indices (i, j, k). Iterate through the array with three nested loops and compute the expression for each combination. Track the maximum value seen so far. This approach guarantees correctness because it evaluates all possibilities, but it quickly becomes impractical for large arrays due to cubic time complexity. It’s useful as a baseline when first reasoning about the problem.
Approach 2: Sorting the Array (Time: O(n log n), Space: O(1) or O(n) depending on implementation)
Sorting the array helps reveal which values contribute most to the expression. Since the maximum result often comes from combining the largest elements with the smallest one, you can sort the array and evaluate only a few candidate combinations such as the largest two values with the smallest value. Sorting reduces the search space dramatically compared to brute force. This approach relies on the observation that extreme values dominate the expression.
Approach 3: Track Maximum, Second Maximum, and Minimum (Greedy) (Time: O(n), Space: O(1))
The optimal solution scans the array once while tracking three key values: the maximum element, the second maximum element, and the minimum element. These values are sufficient because the expression becomes largest when the positive contribution is maximized and the subtractive component is minimized. During a single pass, update these three variables using simple comparisons. After the scan, compute the expression using these extremes. This greedy observation removes the need for sorting or enumeration.
This solution is a classic pattern when working with Array problems: the optimal answer often depends only on a few extreme values. The reasoning also overlaps with Greedy strategies where local optimal choices lead to the global optimum, and sometimes with Sorting when extremes must be identified.
Recommended for interviews: The greedy single-pass approach. Start by explaining the brute force enumeration to demonstrate understanding of the constraints, then derive the insight that only the maximum, second maximum, and minimum elements affect the result. Interviewers expect the final O(n) time and O(1) space solution.
According to the problem description, we need to choose three elements a, b, and c at distinct indices such that the value of the expression a + b - c is maximized.
We only need to traverse the array to find the largest two elements a and b and the smallest element c. Then we can calculate the value of the expression.
The time complexity is O(n), where n is the length of the array. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Enumeration (Brute Force) | O(n^3) | O(1) | Useful for validating logic or very small arrays |
| Sorting + Candidate Evaluation | O(n log n) | O(1)–O(n) | When sorting is acceptable and you want simple reasoning with extreme values |
| Track Max, Second Max, and Min (Greedy) | O(n) | O(1) | Best general solution; single pass without sorting |
Leetcode Weekly Contest 476 Q1. Maximize Expression of Three Elements #python #dsa • ADevOpsBeginner • 207 views views
Watch 8 more video solutions →Practice Maximize Expression of Three Elements with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor