BackmediumArrays TCS

Galactic Resource Allocation 3 Solution

Problem Statement

Given an array of resource quantities for each day and the number of consecutive days a spaceship needs to dock, find the maximum resource quantity that can be allocated to a spaceship.

Example 1
Input
[14, 20, 8, 12, 15, 10, 14, 20, 8, 12, 15, 10]
Output
42

Explanation: To find the maximum sum of a subarray of size 3, we start by considering the first three elements: 14 + 20 + 8 = 42. This is the maximum sum we can get from a subarray of size 3.

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

Explanation: To find the maximum sum of a subarray of size 3, we start by considering the first three elements: 1 + 2 + 3 = 6. However, we can get a larger sum by considering the elements from index 1 to 3: 2 + 3 + 4 = 9. But the maximum sum is actually 4 + 5 + 3 (3 is the next number in the array, not included in the original sum) = 12.

Constraints

  • 1 <= array length <= 1000
  • 1 <= window size <= array length
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free