Pair network nodes to maximize the sum of minimum bandwidths

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.

Amazon logo
Amazon
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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

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

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.

Input: nodes = [1,1,1,1]
Output: 2

All capacities are equal, so every pairing contributes 1 + 1 = 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.