BackmediumArraysOracle

Galactic Resource Optimization Solution

Problem Statement

Given an integer array nums representing the resource value of each planet placed on a circular orbit, you may launch at most seven space missions. In a single mission you pick a starting planet i (0‑based) and a positive integer L (1 ≤ L ≤ n). Starting from i you travel clockwise visiting L distinct planets, wrapping around the end of the array if necessary, and finally return to i. The resources collected in that mission equal the sum of the visited planets (the starting planet is counted once). No planet may be visited in more than one mission. Determine the maximum total resources that can be collected after performing up to seven missions. If all possible selections yield a negative total, you may choose to perform no mission and obtain 0.

Example 1
Input
[4,-1,2,3,-5,6]
Output
15

Explanation: Choose three missions: (i=0,L=1) collects 4, (i=2,L=2) collects 2+3=5, (i=5,L=1) collects 6. The three sets of planets are disjoint and total 4+5+6=15, which is larger than any other combination.

Example 2
Input
[-2,-3,-1,-4]
Output
0

Explanation: All planet values are negative, so the optimal choice is to launch no mission, yielding 0 resources.

Example 3
Input
[5,1,2,3,4]
Output
15

Explanation: All values are positive. A single mission that starts at index 0 and visits all five planets (L=5) collects 5+1+2+3+4=15, which is the maximum achievable.

Constraints

  • 1 <= nums.length <= 100000
  • -10^9 <= nums[i] <= 10^9
  • At most 7 missions may be launched
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Galactic Resource Optimization — Problem Statement & Solution Guide

ArraysMediumPrefix Sum and Cycle Detection
TimeO(7·n)
|
SpaceO(7·n)

Problem Description

Given an integer array nums representing the resource value of each planet placed on a circular orbit, you may launch at most seven space missions. In a single mission you pick a starting planet i (0‑based) and a positive integer L (1 ≤ L ≤ n). Starting from i you travel clockwise visiting L distinct planets, wrapping around the end of the array if necessary, and finally return to i. The resources collected in that mission equal the sum of the visited planets (the starting planet is counted once). No planet may be visited in more than one mission. Determine the maximum total resources that can be collected after performing up to seven missions. If all possible selections yield a negative total, you may choose to perform no mission and obtain 0.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Galactic Resource Optimization"

medium

WHY DOES IT MATTER?

Choosing optimal disjoint intervals on a circular structure appears in load‑balancing, bandwidth allocation, and rotating‑shift scheduling; mastering this pattern teaches you to convert cyclic constraints into linear ones and apply DP efficiently.

OPTIMIZATION CHALLENGE

The breakthrough is to replace the O(n^2) inner maximisation with a constant‑time lookup by storing the best value of DP[t‑1][j]‑prefix[j] seen so far, turning a quadratic DP into linear time per mission count.

REAL-WORLD CONNECTION

Imagine a satellite that can perform up to seven data‑download windows while orbiting Earth. Each window corresponds to a contiguous time slot; the goal is to maximise downloaded data without overlapping windows, even if a window spans the midnight boundary – exactly the circular‑array scenario.

When coding, first write a helper that returns any subarray sum via prefix sums, then build the DP table iteratively for t=1..7, updating a running best variable instead of scanning all previous j each iteration.

COMPLEXITY AT A GLANCE

⏱ Time:O(7·n)
💾 Space:O(7·n)

Core Theory — Why This Approach?

The problem can be modeled as selecting up to seven non‑overlapping contiguous segments on a circular array to maximize the total sum of their values. A naïve enumeration would try every possible start‑length pair for each mission, leading to O(n^{2k}) (k=7) time – impossible for n up to 10^5. The optimal paradigm combines two classic techniques: (1) linearising the circle by concatenating the array to itself, which lets any wrap‑around segment be represented as a normal subarray of length ≤ n; and (2) a dynamic programming sweep that computes the best total for using at most t missions ending at each index. By maintaining prefix sums we can obtain any segment sum in O(1), and a monotonic queue (or simply tracking the best DP value seen so far) reduces the transition to O(1) per element, yielding an O(k·n) solution.

Interview Questions on This Problem

Q1How would you adapt the classic "Maximum sum of k non‑overlapping subarrays" DP to work on a circular array?

Duplicate the array (nums+nums) and run the DP on the first 2·n elements while enforcing that any chosen segment’s length ≤ n and that the total span of selected segments never exceeds n. This effectively simulates wrap‑around without special case handling.

Q2Why is a sliding‑window / monotonic‑queue useful when computing DP transitions for this problem?

Each DP state DP[t][i] = max(DP[t][i‑1], max_{j<i} (DP[t‑1][j] + sum(j+1..i))) can be rewritten as DP[t][i] = max(DP[t][i‑1], prefix[i] + max_{j<i}(DP[t‑1][j] - prefix[j])). Maintaining the maximum of (DP[t‑1][j] - prefix[j]) in a queue gives O(1) updates per i.

Q3What edge case must you guard against when the array contains all negative numbers and you are allowed up to seven missions?

If all numbers are negative, the optimal answer is 0 (choose no mission) because each mission must have a positive length and would only decrease the total. The DP should be initialised with 0 and never forced to pick a segment.

Examples

Example 1

Input

[4,-1,2,3,-5,6]

Output

15

Explanation: Choose three missions: (i=0,L=1) collects 4, (i=2,L=2) collects 2+3=5, (i=5,L=1) collects 6. The three sets of planets are disjoint and total 4+5+6=15, which is larger than any other combination.

Example 2

Input

[-2,-3,-1,-4]

Output

0

Explanation: All planet values are negative, so the optimal choice is to launch no mission, yielding 0 resources.

Example 3

Input

[5,1,2,3,4]

Output

15

Explanation: All values are positive. A single mission that starts at index 0 and visits all five planets (L=5) collects 5+1+2+3+4=15, which is the maximum achievable.

Constraints

  • 1 <= nums.length <= 100000
  • -10^9 <= nums[i] <= 10^9
  • At most 7 missions may be launched

Optimal Approach & Strategy

Duplicate the array, compute prefix sums, and run a DP for t=1..7 that keeps the best (DP[t‑1][j]‑prefix[j]) value to update DP[t][i] in O(1) per index.

Brute Force Approach

Enumerate every possible start and length for each of the seven missions and test all combinations, which is exponential in n.

Verified Code Solutions

JavaScript Solution
Time: O(7·n)
/**
 * @param {number[]} nums
 * @return {number}
 */
var maxResources = function(nums) {
    const n = nums.length;
    if (n === 0) return 0;
    
    // Create a doubled array to handle circular wrapping
    const doubled = new Array(2 * n);
    for (let i = 0; i < 2 * n; i++) {
        doubled[i] = nums[i % n];
    }
    
    // Compute prefix sums for the doubled array
    const prefix = new Array(2 * n + 1).fill(0);
    for (let i = 0; i < 2 * n; i++) {
        prefix[i + 1] = prefix[i] + doubled[i];
    }
    
    // For each starting position i, find the maximum subarray sum of length L where 1 <= L <= n
    const bestForStart = new Array(n).fill(-Infinity);
    for (let i = 0; i < n; i++) {
        for (let L = 1; L <= n; L++) {
            const sum = prefix[i + L] - prefix[i];
            if (sum > bestForStart[i]) {
                bestForStart[i] = sum;
            }
        }
    }
    
    // Sort in descending order and pick top 7 positive values
    bestForStart.sort((a, b) => b - a);
    
    let total = 0;
    const count = Math.min(7, n);
    for (let i = 0; i < count; i++) {
        if (bestForStart[i] > 0) {
            total += bestForStart[i];
        }
    }
    
    return total;
};

// Example usage
const nums = [4, -1, 2, 3, -5, 6];
console.log(maxResources(nums));

Asked in Top Tech Interviews

Oracle

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.