[LeetCode] Maximum Median Sum of Subsequences of Size 3

3627. Maximum Median Sum of Subsequences of Size 3

You are given an integer array nums with a length divisible by 3.

You want to make the array empty in steps. In each step, you can select any three elements from the array, compute their median, and remove the selected elements from the array.

The median of an odd-length sequence is defined as the middle element of the sequence when it is sorted in non-decreasing order.

Return the maximum possible sum of the medians computed from the selected elements.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
long long maximumMedianSum(vector<int>& nums) {
int n = nums.size();
sort(begin(nums), end(nums));
long long res = 0;
for(int i = n / 3; i < n; i += 2) res += nums[i];
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/maximum-median-sum-of-subsequences-of-size-3/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.