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.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
Input: nums = [2,1,3,2,1,3]
Output: 5
Explanation:
nums becomes [2, 1, 2].nums becomes empty.Hence, the sum of the medians is 3 + 2 = 5.
Example 2:
Input: nums = [1,1,10,10,10,10]
Output: 20
Explanation:
nums becomes [1, 10, 10].nums becomes empty.Hence, the sum of the medians is 10 + 10 = 20.
Constraints:
1 <= nums.length <= 5 * 105nums.length % 3 == 01 <= nums[i] <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Greedy
[2,1,3,2,1,3]
[1,1,10,10,10,10]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3627: Maximum Median Sum of Subsequences of Size 3
class Solution {
public long maximumMedianSum(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
long ans = 0;
for (int i = n / 3; i < n; i += 2) {
ans += nums[i];
}
return ans;
}
}
// Accepted solution for LeetCode #3627: Maximum Median Sum of Subsequences of Size 3
func maximumMedianSum(nums []int) (ans int64) {
sort.Ints(nums)
n := len(nums)
for i := n / 3; i < n; i += 2 {
ans += int64(nums[i])
}
return
}
# Accepted solution for LeetCode #3627: Maximum Median Sum of Subsequences of Size 3
class Solution:
def maximumMedianSum(self, nums: List[int]) -> int:
nums.sort()
return sum(nums[len(nums) // 3 :: 2])
// Accepted solution for LeetCode #3627: Maximum Median Sum of Subsequences of Size 3
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3627: Maximum Median Sum of Subsequences of Size 3
// class Solution {
// public long maximumMedianSum(int[] nums) {
// Arrays.sort(nums);
// int n = nums.length;
// long ans = 0;
// for (int i = n / 3; i < n; i += 2) {
// ans += nums[i];
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3627: Maximum Median Sum of Subsequences of Size 3
function maximumMedianSum(nums: number[]): number {
nums.sort((a, b) => a - b);
const n = nums.length;
let ans = 0;
for (let i = n / 3; i < n; i += 2) {
ans += nums[i];
}
return ans;
}
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: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
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.