Quick Overview

Given a sorted array of node bandwidth capacities with even length, pair every node exactly once so that the sum of each pair's smaller capacity is as large as possible. It tests greedy reasoning, an exchange argument for correctness, and care with 64-bit totals.

Pair network nodes to maximize the sum of minimum bandwidths

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an integer array `nodes` holding the bandwidth capacities of network nodes, sorted in non-decreasing order. The length n of the array is even. Split the nodes into n / 2 pairs so that every node belongs to exactly one pair. Each pair has a primary node and a secondary node, and the bandwidth contribution of a pair is the minimum of the two nodes' capacities. Return the maximum total bandwidth contribution you can reach over all possible pairings. ### Function Signature ```python def max_bandwidth(nodes: list[int]) -> int: ``` ### Rules - Every node is used in exactly one pair, so there are exactly n / 2 pairs. - Which node of a pair is the primary and which is the secondary does not change the pair's contribution. - Return the maximum total as an integer. ### Constraints - `2 <= len(nodes) <= 10^5`, and `len(nodes)` is even - `1 <= nodes[i] <= 10^9` - `nodes` is sorted in non-decreasing order ### Examples ```text Input: nodes = [1,2,3,4,5,6] Output: 9 ``` The pairing (1,2), (3,4), (5,6) contributes 1 + 3 + 5 = 9, and no pairing does better. The pairing (1,4), (2,5), (3,6), for comparison, contributes only 1 + 2 + 3 = 6. ```text Input: nodes = [1,1,1,1] Output: 2 ``` All capacities are equal, so every pairing contributes 1 + 1 = 2. ```text Input: nodes = [2,2,5,9,10,10] Output: 17 ``` The pairing (2,2), (5,9), (10,10) contributes 2 + 5 + 10 = 17. Splitting the two 10s, as in (2,10), (2,10), (5,9), drops the total to 2 + 2 + 5 = 9.

Overview: Given a sorted array of node bandwidth capacities with even length, pair every node exactly once so that the sum of each pair's smaller capacity is as large as possible. It tests greedy reasoning, an exchange argument for correctness, and care with 64-bit totals.

You are given an integer array `nodes` holding the bandwidth capacities of network nodes, sorted in non-decreasing order. The length n of the array is even. Split the nodes into n / 2 pairs so that every node belongs to exactly one pair. Each pair has a primary node and a secondary node, and the bandwidth contribution of a pair is the minimum of the two nodes' capacities. Which node of a pair is the primary and which is the secondary does not change the pair's contribution. Return the maximum total bandwidth contribution you can reach over all possible pairings, as an integer. The total can exceed 2^31 - 1 (it is at most 5 * 10^13), so the Java method returns `long` and the C++ method returns `long long`. ### Example 1 ```text Input: nodes = [1,2,3,4,5,6] Output: 9 ``` The pairing (1,2), (3,4), (5,6) contributes 1 + 3 + 5 = 9, and no pairing does better. The pairing (1,4), (2,5), (3,6), for comparison, contributes only 1 + 2 + 3 = 6. ### Example 2 ```text Input: nodes = [2,2,5,9,10,10] Output: 17 ``` The pairing (2,2), (5,9), (10,10) contributes 2 + 5 + 10 = 17. Splitting the two 10s, as in (2,10), (2,10), (5,9), drops the total to 2 + 2 + 5 = 9. ### Constraints - `2 <= len(nodes) <= 10^5`, and `len(nodes)` is even - `1 <= nodes[i] <= 10^9` - `nodes` is sorted in non-decreasing order

Constraints

  • 2 <= len(nodes) <= 10^5, and len(nodes) is even
  • 1 <= nodes[i] <= 10^9
  • nodes is sorted in non-decreasing order

Examples

Input: ([1, 1],)

Expected Output: 1

Explanation: Smallest valid input: one pair of equal values at the lower bound.

Input: ([3, 8],)

Expected Output: 3

Explanation: One pair of distinct values contributes the smaller value.

Hints

  1. The node with the smallest capacity contributes exactly its own capacity to whichever pair it joins. Ask which partner choice gives up the least.
  2. Work through [1, 2, 3, 4] by hand: list all three possible pairings and compare their totals.
  3. The total can reach 5 * 10^13, past the signed 32-bit range, so choose an accumulator type that holds it.

Maximum Total Bandwidth from Pairing Sorted Nodes

easyAmazonSoftware Engineer

You are given an integer array nodes holding the bandwidth capacities of network nodes, sorted in non-decreasing order. The length n of the array is even.

Split the nodes into n / 2 pairs so that every node belongs to exactly one pair. Each pair has a primary node and a secondary node, and the bandwidth contribution of a pair is the minimum of the two nodes' capacities. Which node of a pair is the primary and which is the secondary does not change the pair's contribution.

Return the maximum total bandwidth contribution you can reach over all possible pairings, as an integer.

The total can exceed 2^31 - 1 (it is at most 5 * 10^13), so the Java method returns long and the C++ method returns long long.

Example 1

Input: nodes = [1,2,3,4,5,6]
Output: 9

The pairing (1,2), (3,4), (5,6) contributes 1 + 3 + 5 = 9, and no pairing does better. The pairing (1,4), (2,5), (3,6), for comparison, contributes only 1 + 2 + 3 = 6.

Example 2

Input: nodes = [2,2,5,9,10,10]
Output: 17

The pairing (2,2), (5,9), (10,10) contributes 2 + 5 + 10 = 17. Splitting the two 10s, as in (2,10), (2,10), (5,9), drops the total to 2 + 2 + 5 = 9.

Constraints

  • 2 <= len(nodes) <= 10^5, and len(nodes) is even
  • 1 <= nodes[i] <= 10^9
  • nodes is sorted in non-decreasing order

Examples

Example 1
Input
([1, 1],)
Output
1
Notes
Smallest valid input: one pair of equal values at the lower bound.
Example 2
Input
([3, 8],)
Output
3
Notes
One pair of distinct values contributes the smaller value.

Constraints

  • •2 <= len(nodes) <= 10^5, and len(nodes) is even
  • •1 <= nodes[i] <= 10^9
  • •nodes is sorted in non-decreasing order

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def max_bandwidth(nodes: list[int]) -> int:
Sample call
max_bandwidth([1, 1])
Language
Case 1
Input
([1, 1],)
Expected
1
Case 2
Input
([3, 8],)
Expected
3

Browse More Questions

Show the approach

Approach

The array is already sorted, so pair neighbors: (nodes[0], nodes[1]), (nodes[2], nodes[3]), and so on. Each such pair contributes its first element, so the answer is the sum of nodes[i] over every even index i.

To see that this is optimal, note that nodes[0] is the smallest value, so its pair always contributes exactly nodes[0]. Suppose an optimal pairing puts nodes[0] with nodes[j] for some j >= 2 and nodes[1] with nodes[k], where k >= 2 as well. Both nodes[j] and nodes[k] are at least nodes[1], so those two pairs contribute nodes[0] + nodes[1]. Swapping partners to (nodes[0], nodes[1]) and (nodes[j], nodes[k]) gives nodes[0] + min(nodes[j], nodes[k]), which is at least nodes[0] + nodes[1]. So some optimal pairing joins nodes[0] with nodes[1], and the same argument applies to the remaining n - 2 sorted values by induction.

A single pair returns nodes[0]. Duplicates need no special handling because equal values are interchangeable. The total can reach 5 * 10^13, so the accumulator is 64-bit in Java and C++; JavaScript numbers hold it exactly because it stays below 2^53.

Time complexity:
O(n)
Space complexity:
O(1)