DIY: Maximum Sum of Three Non-Overlapping Arrays

Solve the interview question "Maximum Sum of Three Non-Overlapping Arrays" yourself in this lesson.

We'll cover the following

Problem statement

In this challenge, you are given an array of integers as input. Your task is to find three non-overlapping subarrays of the given array that have the maximum sum. The subarray should be of size k. Our goal is to maximize the sum of all 3*k entries.

The function should return the result as an array of indices representing the starting position of each subarray.

Note: If there are multiple answers, return the lexicographically smallest one.

Input

The function will have two inputs: an integer array called numbers and an integer value called k. The following is an example of the inputs:

numbers = [0,2,1,3,1,7,11,5,5]
k = 2

Output

The function’s output will be an array containing the indices of the starting position of each subarray. These subarrays should each contain the maximum sum. The following is the output for the inputs mentioned above.

[2, 5, 7]

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.