Maximum Sum After Removing K Corner Elements
Given an array arr
of 'N' integer elements, your goal is to remove 'K' elements from either the beginning or the end of the array. The task is to return the maximum possible sum of the elements that remain in the array after these removals.
Note: You can choose to remove elements from both the start and end, but the total number of elements removed must be exactly 'K'.
Input:
The first line contains an integer T representing the number of test cases. Each test case consists of two lines:
- The first line contains two integers N and K, describing the length of the array and the number of elements to be removed, respectively.
- The second line contains N integers, representing the elements of the array arr
.
Output:
For each test case, output a single integer representing the maximum sum of the remaining array elements.
Each output should be on a separate line.
Example:
Input:
2
7 3
1 2 3 4 5 6 7
8 4
5 3 1 1 8 8 2 2
Output:
22
20
Explanation:
For the first test case, by removing the first three elements, the sum of remaining elements {4, 5, 6, 7} is 22. Any other combination results in a lesser sum.
For the second test case, removing the first four elements results in the sum of {8, 8, 2, 2} being 20.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 10^4
0 ≤ K ≤ N
0 ≤ arr[i] ≤ 10^6
Time limit: 1 sec
Be the first one to answer
Add answer anonymously...
Top Jio Platforms Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Top HR questions asked in Jio Platforms Software Engineer
>
Jio Platforms Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app