All problems
1120MediumLinked ListTwo PointersStack

The Heaviest Coupled Wagons

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2130Maximum Twin Sum of a Linked List

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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