Skip to main content

Amazon Server Throughput

easy
Software engineer

You are given an array hostThroughput with $N$ elements, where each element represents the throughput of a host server. Your task is to form as many clusters as possible, with each cluster consisting of exactly three distinct servers.

The throughput of a cluster is defined as the median of the three throughput values. Each server can belong to at most one cluster, and any leftover servers will remain unused.

The goal is to maximize the overall system throughput, which is the sum of the medians from all clusters.

Example 1

Input

hostThroughput = [8, 6, 3, 4, 4, 5, 6]

Output

11

Explanation

To maximize throughput, we want the medians to be as large as possible. 
1. Sort the throughputs: [3, 4, 4, 5, 6, 6, 8]. 
2. With 7 servers, we can form at most floor(7/3) = 2 clusters.
3. Cluster 1: [3, 6, 8] -> Median = 6.
4. Cluster 2: [4, 5, 6] -> Median = 5.
5. Total throughput = 6 + 5 = 11. One server (4) is left unused.

Example 2

Input

hostThroughput = [9, 8, 7, 6, 5, 4]

Output

14

Explanation

Clusters: [4, 8, 9] (median 8) and [5, 6, 7] (median 6). Sum = 14.

Example 3

Input

hostThroughput = [100, 1, 10, 5, 50, 20]

Output

60

Explanation

Clusters: [1, 50, 100] (median 50) and [5, 10, 20] (median 10). Sum = 60.

Constraints

  • 3 <= hostThroughput.length <= 10⁵
  • 1 <= hostThroughput[i] <= 10⁹
  • Total number of clusters is floor(hostThroughput.length / 3).

OA

ArrayGreedySorting
Language
Code editor loads in the browser.

Output

Input

[8,6,3,4,4,5,6]

Expected

11

Your output

Run to see your output.