Maximum Twin Sum of a Linked List
Real-world version: The Heaviest Coupled WagonsThe 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.
def pair_sum(chain: list[int]) -> int:public int pairSum(int[] chain)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's