Remove K Corner Elements - Problem Statement

Given an array "arr" consisting of "N" integer elements, your task is to remove "K" elements from the beginning or the end of the array. You must return the maximum possible sum of the remaining elements in the array.

Note: You may remove elements from both the beginning and the end, but the total number of elements removed must be exactly "K".

Input:

The first line contains a single integer ‘T’ denoting the number of test cases. Each test case includes: 
1. A line containing two integers 'N' and 'K', where N is the number of elements in the array, and K is the number of elements to be removed.
2. A line containing N integers, representing the array elements arr[i].

Output:

For each test case, print a single integer denoting the maximum sum of the remaining elements of the array. The output for each test case will be printed on a separate line.

Example:

Sample Input:
2
6 3
1 2 6 4 5 3
8 4
5 3 1 1 8 8 2 2
Sample Output:
15
20
Explanation:
For test case 1: After removing two elements from the beginning and one element from the end, the array becomes {6, 4, 5}. The sum is 15. 
For test case 2: After removing four elements from the beginning, the array becomes {8, 8, 2, 2}. The sum is 20.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^4
  • 0 <= K <= N
  • 0 <= arr[i] <= 10^6
  • Time limit: 1 sec
AnswerBot
2d

Given an array, remove K elements from beginning or end to maximize sum of remaining elements.

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

  • Calculate sum of re...read more

Help your peers!
Add answer anonymously...
Google Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter