Company: Google_8nov
Difficulty: medium
Maximise sum Problem Description You are given the following: An integer array A of length N An integer K Task For each index i such that 1 ≤ i ≤ N , determine the maximum sum that can be obtained by using at most K elements from the subarray A[1] to A[i] . Notes Assume 1-based indexing. A subarray is a contiguous part of the array. An array that is inside another array. For example, consider the array [1, 2, 3] . It has 6 subarrays: [1] , [2] , [3] , [1,2] , [2,3] , [1,2,3] Example Walkthrough Let's consider an example with: N = 4 K = 2 A = [-1, -2, 2, -4] Approach (for the above example) For index i = 1, the subarray is [-1] : The maximum sum is 0 obtained by taking 0 elements. For index i = 2, the subarray is [-1 -2] : The maximum sum is 0 obtained by taking 0 elements. For index i = 3, the subarray is [-1 -2 2] : The maximum sum is 2 obtained by taking 1 element. For index i = 4, the subarray is [-1 -2 2 -4] : The maximum sum is 2 obtained by taking 1 element. Therefore, the