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.

Example 1
Inputnums = [2,1,3,2,1,3]
Output5
Selecting triples with medians 3 and 2 gives the maximum sum, so the total is 3 + 2 = 5.
Example 2
Inputnums = [1,1,10,10,10,10]
Output20
Both selected triples can have median 10, so the maximum sum is 10 + 10 = 20.

Constraints

  • 1 <= nums.length <= 5 * 10^5
  • nums.length % 3 == 0
  • 1 <= nums[i] <= 10^9

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate