3627. Maximum Median Sum of Subsequences of Size 3 Medium

@problem@discussion
#Array#Math#Greedy#Sorting#Game Theory



1/**
2 * [3627] Maximum Median Sum of Subsequences of Size 3
3 *
4 * You are given an integer array nums with a length divisible by 3.
5 * 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.
6 * The median of an odd-length sequence is defined as the middle element of the sequence when it is sorted in non-decreasing order.
7 * Return the maximum possible sum of the medians computed from the selected elements.
8 *  
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">nums = [2,1,3,2,1,3]</span>
12 * Output: <span class="example-io">5</span>
13 * Explanation:
14 * 
15 * 	In the first step, select elements at indices 2, 4, and 5, which have a median 3. After removing these elements, nums becomes [2, 1, 2].
16 * 	In the second step, select elements at indices 0, 1, and 2, which have a median 2. After removing these elements, nums becomes empty.
17 * 
18 * Hence, the sum of the medians is 3 + 2 = 5.
19 * </div>
20 * <strong class="example">Example 2:
21 * <div class="example-block">
22 * Input: <span class="example-io">nums = [1,1,10,10,10,10]</span>
23 * Output: <span class="example-io">20</span>
24 * Explanation:
25 * 
26 * 	In the first step, select elements at indices 0, 2, and 3, which have a median 10. After removing these elements, nums becomes [1, 10, 10].
27 * 	In the second step, select elements at indices 0, 1, and 2, which have a median 10. After removing these elements, nums becomes empty.
28 * 
29 * Hence, the sum of the medians is 10 + 10 = 20.
30 * </div>
31 *  
32 * Constraints:
33 * 
34 * 	1 <= nums.length <= 5 * 10^5
35 * 	nums.length % 3 == 0
36 * 	1 <= nums[i] <= 10^9
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/maximum-median-sum-of-subsequences-of-size-3/
42// discuss: https://leetcode.com/problems/maximum-median-sum-of-subsequences-of-size-3/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn maximum_median_sum(nums: Vec<i32>) -> i64 {
48        
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_3627() {
60    }
61}
62

Back
© 2026 bowen.ge All Rights Reserved.