You are given a cyclic array nums and an integer k.
Partition nums into at most k subarrays. As nums is cyclic, these subarrays may wrap around from the end of the array back to the beginning.
The range of a subarray is the difference between its maximum and minimum values. The score of a partition is the sum of subarray ranges.
Return the maximum possible score among all cyclic partitions.
Example 1:
Input: nums = [1,2,3,3], k = 2
Output: 3
Explanation:
nums into [2, 3] and [3, 1] (wrapped around).[2, 3] is max(2, 3) - min(2, 3) = 3 - 2 = 1.[3, 1] is max(3, 1) - min(3, 1) = 3 - 1 = 2.1 + 2 = 3.Example 2:
Input: nums = [1,2,3,3], k = 1
Output: 2
Explanation:
nums into [1, 2, 3, 3].[1, 2, 3, 3] is max(1, 2, 3, 3) - min(1, 2, 3, 3) = 3 - 1 = 2.Example 3:
Input: nums = [1,2,3,3], k = 4
Output: 3
Explanation:
Identical to Example 1, we partition nums into [2, 3] and [3, 1]. Note that nums may be partitioned into fewer than k subarrays.
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 1091 <= k <= nums.lengthProblem Overview: You are given an array arranged in a cycle. The task is to partition this circular sequence into contiguous segments so the total partition score is maximized. Because the array is cyclic, partitions may wrap around the end, which means you must carefully handle rotations and overlapping ranges.
Approach 1: Brute Force Enumeration (Exponential Time, O(2^n) time, O(n) space)
The most direct strategy is to try every possible way to cut the array into segments. Since the array is cyclic, you must also consider every possible starting index (rotation) and then enumerate all partition combinations for that linearized version. For each partition configuration, compute the segment score and track the maximum. This approach quickly becomes infeasible because the number of partition patterns grows exponentially with n. It is mainly useful to verify correctness on very small inputs or to understand the structure of the search space.
Approach 2: Dynamic Programming on Linearized Cycles (O(n^2) time, O(n) space)
The key observation is that a cyclic array can be handled by duplicating the array (arr + arr) and evaluating partitions over windows of length n. Once the array is linearized this way, dynamic programming can compute the optimal score for prefixes. Define a DP state such as dp[i] representing the best score achievable for partitions ending at index i. For every possible previous cut position j, evaluate the score of the segment (j+1..i) and update dp[i]. Efficient segment score computation typically relies on precomputation like prefix sums or range statistics. Iterate over valid window boundaries so partitions never exceed length n. This reduces the search from exponential possibilities to a quadratic DP transition.
The duplicated-array trick is a standard way to handle circular structures. Instead of writing special wrap-around logic, you convert the problem into a sliding window over the doubled sequence. Many circular array problems in array processing follow the same pattern.
State transitions are straightforward: iterate i as the right boundary, then scan backward to evaluate candidate segments. The DP stores the best score so far while respecting the cyclic window constraint. This pattern appears frequently in partition problems involving dynamic programming, especially when segments depend on statistics such as sums, minima, or maxima.
Recommended for interviews: Interviewers expect the dynamic programming solution with the doubled-array trick. Showing the brute force approach first demonstrates understanding of the cyclic partition search space, but the optimized DP approach shows you can reduce exponential branching to structured state transitions. Recognizing when to convert a circular structure into a linear window is a common optimization in array and partition DP problems.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Partition Enumeration | O(2^n) | O(n) | Useful only for small arrays or validating logic during development |
| Dynamic Programming with Cyclic Linearization | O(n^2) | O(n) | General solution for circular partition optimization problems |
Q4. Maximize Cyclic Partition Score || Adhoc, Cyclic Subarray || Leetcode Biweekly 475 || Watch 2X 🚀 • Rajan Keshari ( CSE - IIT Dhanbad ) • 952 views views
Watch 2 more video solutions →Practice Maximize Cyclic Partition Score with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor