Show problems as
Classic problem #2130MediumLinked ListTwo PointersStack

Maximum Twin Sum of a Linked List

Real-world version: The Heaviest Coupled Wagons
Tracked in this browser only
Read Maximum Twin Sum of a Linked List on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A chain of wagons runs from front to back and holds an even number of them. Counting from zero, wagon i is coupled with wagon n - 1 - i, so the front wagon pairs with the back one, the second with the second from the back, and so on.

Because the harness passes plain JSON, the chain reaches you as chain, listing the loads in order from the front wagon.

A couple's weight is the sum of its two wagons' loads. Return the largest weight any couple carries.

Examples

Example 1

Input
chain = [1, 100, 100, 1]
Output
200

The two outer wagons couple for a weight of 2 and the two inner ones for 200.

Example 2

Input
chain = [1, 2, 3, 4, 5, 6]
Output
7

Every couple weighs 7: the first with the last, the second with the fifth and the third with the fourth.

Example 3

Input
chain = [1, 2]
Output
3

The chain holds a single couple, so its weight is the answer.

Constraints

  • chain.length is even
  • 2 <= chain.length <= 10^5
  • 1 <= chain[i] <= 10^5

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def pair_sum(chain: list[int]) -> int:
Java
public int pairSum(int[] chain)