Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,4,2,5]
[-2,0,5,-2,4]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3745: Maximize Expression of Three Elements
class Solution {
public int maximizeExpressionOfThree(int[] nums) {
final int inf = 1 << 30;
int a = -inf, b = -inf, c = inf;
for (int x : nums) {
if (x < c) {
c = x;
}
if (x >= a) {
b = a;
a = x;
} else if (x > b) {
b = x;
}
}
return a + b - c;
}
}
// Accepted solution for LeetCode #3745: Maximize Expression of Three Elements
func maximizeExpressionOfThree(nums []int) int {
const inf = 1 << 30
a, b, c := -inf, -inf, inf
for _, x := range nums {
if x < c {
c = x
}
if x >= a {
b = a
a = x
} else if x > b {
b = x
}
}
return a + b - c
}
# Accepted solution for LeetCode #3745: Maximize Expression of Three Elements
class Solution:
def maximizeExpressionOfThree(self, nums: List[int]) -> int:
a = b = -inf
c = inf
for x in nums:
if x < c:
c = x
if x >= a:
a, b = x, a
elif x > b:
b = x
return a + b - c
// Accepted solution for LeetCode #3745: Maximize Expression of Three Elements
fn maximize_expression_of_three(nums: Vec<i32>) -> i32 {
let mut nums = nums;
nums.sort_unstable();
let len = nums.len();
nums[len - 1] + nums[len - 2] - nums[0]
}
fn main() {
let ret = maximize_expression_of_three(vec![1, 4, 2, 5]);
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(maximize_expression_of_three(vec![1, 4, 2, 5]), 8);
assert_eq!(maximize_expression_of_three(vec![-2, 0, 5, -2, 4]), 11);
}
// Accepted solution for LeetCode #3745: Maximize Expression of Three Elements
function maximizeExpressionOfThree(nums: number[]): number {
const inf = 1 << 30;
let [a, b, c] = [-inf, -inf, inf];
for (const x of nums) {
if (x < c) {
c = x;
}
if (x >= a) {
b = a;
a = x;
} else if (x > b) {
b = x;
}
}
return a + b - c;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.