Sitemap
Roya

Roya

A research scientist focused on responsible AI, with a love for nature, history, women's rights, and photography. Roya means "a sweet dream" in Farsi.

Member-only story

Maximum Sum Circular Subarray

3 min readMar 3, 2025

--

This blog series attempts to solve the 500 Top Leet Code Interview Questions with the help of AI Code Assistance, such as Gemini and GPT.

Question 128:

Maximum Sum Circular Subarray

We are given a circular integer array nums, and we need to determine the maximum possible sum of a non-empty subarray. The circular property means that elements at the end of the array are adjacent to elements at the beginning.

  • The subarray must be contiguous in the original array order.
  • The subarray can wrap around (i.e., take elements from the end and the beginning).
  • We need to consider both normal subarrays and circular subarrays.

Breakdown:

To find the maximum sum subarray in a circular array, we need to consider two cases:

  1. The maximum subarray is a normal subarray (does not wrap around). This can be found using Kadane’s Algorithm.
  2. The maximum subarray is a circular subarray, meaning it wraps around. This is equivalent to:
    - Taking the sum of all elements in nums, total_sum.
    - Finding the minimum sum subarray using Kadane’s Algorithm and subtracting it from total_sum.

Approach:

  1. Find the maximum subarray sum using Kadane’s Algorithm (max_kadane).
  2. Find the minimum subarray sum using Kadane’s Algorithm (min_kadane).

--

--

Roya
Roya

Written by Roya

A research scientist focused on responsible AI, with a love for nature, history, women's rights, and photography. Roya means "a sweet dream" in Farsi.