A quiz app ranks an even number of players in a linked list, best first. For a team round it pairs players from both ends: the 1st with the last, the 2nd with the second-to-last, and so on. A pair's score is the sum of the two players' points.
Write bestPairScore(head) that returns the highest pair score. Node i (counting from 0) is paired with node n - 1 - i, where n is the number of nodes.
Input: head = [5,4,2,1] Output: 6
The pairs are 5 + 1 = 6 and 4 + 2 = 6, so the best is 6.
Input: head = [4,2,2,3] Output: 7
The pairs are 4 + 3 = 7 and 2 + 2 = 4.
2 to 10^5.1 <= node.val <= 10^5Lists are written as arrays here, like [1, 2, 3]. Your function works with ListNode objects, each with val and next.
Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #234 Best Paired Score. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.