v9.1.1
Menu

Maximum Sum Circular Subarray

mediumPROStreaming & QueuesNetflix DSA

Given a circular integer array nums, find the maximum possible sum of a non-empty subarray.

A circular subarray can wrap around the end to the beginning.

Example

Input
1,-2,3,-2
Output
3
Reasoning
  • The input array is nums = [1, -2, 3, -2], and we need to find the maximum sum of a non-empty subarray.
  • We consider all possible subarrays, including those that wrap around the end to the beginning, and calculate their sums:
    • Subarray [1] has sum 11,
    • subarray [-2] has sum −2-2,
    • subarray [3] has sum 33,
    • subarray [-2] has sum −2-2,
    • subarray [1, -2] has sum 1+(−2)=−11 + (-2) = -1,
    • subarray [1, -2, 3] has sum 1+(−2)+3=21 + (-2) + 3 = 2,
    • subarray [1, -2, 3, -2] has sum 1+(−2)+3+(−2)=01 + (-2) + 3 + (-2) = 0,
    • subarray [-2, 3] has sum −2+3=1-2 + 3 = 1,
    • subarray [-2, 3, -2] has sum −2+3+(−2)=−1-2 + 3 + (-2) = -1,
    • subarray [3, -2] has sum 3+(−2)=13 + (-2) = 1,
    • subarray [3, -2, 1] has sum 3+(−2)+1=23 + (-2) + 1 = 2,
    • subarray [-2, 1] has sum −2+1=−1-2 + 1 = -1.
  • The maximum sum of a subarray is 33, which is obtained from the subarray [3].
  • The final output is 33.

Constraints

  • 1 <= len(nums) <= 3 * 10^4
  • -3 * 10^4 <= nums[i] <= 3 * 10^4

More from Netflix DSA

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.