Ctrl K

Best Run on a Ring

Medium
The numbers of values are arranged in a circle: after the last element comes the first one again. Choose a non-empty run of consecutive elements on this ring (it may wrap past the end of the array to the start) and return the largest possible sum. Each element can appear in the run at most once, so the run is no longer than len(values).
Example 1
values=[6, -10, 2, 3] → 11

A segment may wrap past the end back to the start: 2 + 3 + 6 = 11.

Example 2
values=[-1, 4, -2, 5, -9] → 7

The best segment here does not wrap: 4 - 2 + 5 = 7.

Example 3
values=[-8, -3, -5] → -3

All numbers are negative and the segment cannot be empty, so take -3.

Constraints
  • 1 ≤ len(values) ≤ 3·10⁴
  • -3·10⁴ ≤ values[i] ≤ 3·10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRising Chain With Small Steps HardBest Score With Short Jumps MediumMarket Fruit Bonus MediumMaximum Sum With Short Hops HardFerry Runs From the Depot Hard
loading Python…
Input
values=[6, -10, 2, 3]
Expected
11

Run checks the first 3 tests in your browser. Submit checks all 120 tests on the server, hidden ones included.