Loading...
You are given an integer array nums arranged in a circle: positions i and i+1 are adjacent, and so are the first and last positions.
Choose a subset of positions such that at most one pair of adjacent positions is chosen across the entire selection. (In particular, three consecutive chosen positions are impossible, since that would contain two adjacent pairs.) For nums.length = 2 the two positions count as a single adjacency.
Return the maximum possible sum of the chosen values.
Input: nums = [9, 8, 1, 2]
Output: 17
Explanation: Choose positions 0 and 1 (9 + 8). They are adjacent, and that is the
one allowed pair. No larger selection is valid.
Input: nums = [5, 4, 3]
Output: 9
Explanation: In a circle of three, every two positions are adjacent. Choosing
5 and 4 uses the single allowed pair. All three would form three adjacent
pairs, which is not allowed.
nums.length ≤105nums[i] ≤104Click "Run" to test with sample cases or "Submit" to run all tests.