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

AnswerBot
4mo

Given an array of integers, remove K elements from either end to maximize sum of remaining elements.

  • Iterate through all possible combinations of removing K elements from start and end

  • Calculate sum of ...read more

Help your peers!
Select
Add answer anonymously...

Jio Platforms Software Engineer interview questions & answers

A Software Engineer was asked Q. What are the features of Java 8?
A Software Engineer was asked Q. What data types are available in Python?
A Software Engineer was asked 10mo agoQ. Past Projects Design

Popular interview questions of Software Engineer

A Software Engineer was asked Q1. What are the features of Java 8?
A Software Engineer was asked Q2. What data types are available in Python?
A Software Engineer was asked Q3. What is encapsulation?
Jio Platforms Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits