InferenceAI

Enter at least two characters to search.

Start free

Circular Maximum Sum

mediumArrays & Strings25 min25 XP

Treat nums as circular (the end connects back to the start). Return the maximum sum of a non-empty subarray, where a subarray may wrap around but may not use any element twice.

Requirements

  • O(n)O(n): the answer is either an ordinary Kadane maximum, or the total minus the minimum subarray.

Examples

EXAMPLE WALKTHROUGHno wrap

A guided view of the published example, not a trace of your code.

INPUT[[1,-2,3,-2]]
TASKcircular_max_sum()
EXPECTED OUTPUT3

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [1,-2,3,-2]

Output: 3

Explanation: no wrap

Input: nums = [5,-3,5]

Output: 10

Explanation: wraps

Input: nums = [-3,-2,-3]

Output: -2

Explanation: all negative

Hint 1

A wrapping subarray is the complement of a non-wrapping one.

Hint 2

If every number is negative, the wrap formula would choose an empty subarray, so return the ordinary maximum.

Topics

DSAArrays & StringsPython