[LeetCode] Maximize Expression of Three Elements

3745. Maximize Expression of Three Elements

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.

1
2
3
4
5
6
7
8
class Solution {
public:
int maximizeExpressionOfThree(vector<int>& nums) {
sort(begin(nums), end(nums));
int n = nums.size();
return nums[n-1] + nums[n-2] - nums[0];
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/21/PS/LeetCode/maximize-expression-of-three-elements/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.