DSA · Arrays & Dynamic Programming · #114
Maximum Sum Circular Subarray
Module 75 · difficulty 3/5·⏱ 30:00starts on first keystroke
Given a **circular** integer array `nums` of length `n`, return the maximum possible sum of a non-empty subarray. A circular array means the end connects to the beginning: the element after `nums[n-1]` is `nums[0]`. A subarray may wrap around the end, but it may include each element **at most once** (its length is at most `n`). Implement `function maxSubarraySumCircular(nums)` returning the maximum subarray sum.
Examples
nums = [1,-2,3,-2]→3— Subarray [3] has the largest sum 3.nums = [5,-3,5]→10— Subarray [5,5] wraps around: nums[2] + nums[0] = 10.nums = [-3,-2,-3]→-2— All negative; the best single element is -2.
Constraints
- · n == nums.length
- · 1 <= n <= 3 * 10^4
- · -3 * 10^4 <= nums[i] <= 3 * 10^4
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.