Maximum Sum of 3 Non-overlapping Subarrays
Maximum Sum of 3 Non-overlapping Subarrays
Let's have a little adventure exploring arrays and sums! In this challenge, your task is to find three non-overlapping subarrays within a given list of numbers. These subarrays should collectively have the maximum possible sum. The challenge will help you practice loops, lists, and a bit of math!
What to do:
Given an array of numbers and a fixed length k, find three non-overlapping subarrays of length k which have the maximum total sum. Return the starting indices of these subarrays in the order they appear in the array.
Example:
Imagine you have a magic list of numbers like:
nums = [1, 2, 1, 2, 6, 7, 5, 1] k = 2
Your function should find subarrays of length 2:
- Subarray from index 0: ([1, 2])
- Subarray from index 1: ([2, 1])
- Subarray from index 2: ([1, 2])
- Subarray from index 3: ([2, 6])
- Subarray from index 4: ([6, 7])
- Subarray from index 5: ([7, 5])
- Subarray from index 6: ([5, 1])
Find three non-overlapping ones that give you the highest sum total!
For this example, the correct subarrays indices are:
- Start at index 0 ([1, 2]),
- Start at index 3 ([2, 6]),
- Start at index 5 ([7, 5]).
Thus, your function should return [0, 3, 5].
Getting Started:
- You will receive two parameters: the list of numbers
numsandk. - Write a function that will return a list of indices of the three subarrays.
Have fun coding!
Inputs
Arg 1:
[1,2,1,2,6,7,5,1]Arg 2:
2Expected Output
[0, 3, 5]Actual Output