# Maximum sum after K consecutive deletions

Given an array arr[] of size N and an integer K, the task is to delete K continuous elements from the array such that the sum of the remaining element is maximum. Here we need to print remaining elements of array.

Examples:

Input: arr[] = {-1, 1, 2, -3, 2, 2}, K = 3
Output: -1 2 2
Delete 1, 2, -3 and the sum of the remaining
elements will be 3 which is maximum possible.

Input: arr[] = {1, 2, -3, 4, 5}, K = 1
Output: 1 2 4 5

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Calculate the sum of k-consecutive elements and remove the elements with the minimum sum. Print the rest of the elements of the array.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print the array after removing ` `// k consecutive elements such that the sum ` `// of the remaining elements is maximized ` `void` `maxSumArr(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``int` `cur = 0, index = 0; ` ` `  `    ``// Find the sum of first k elements ` `    ``for` `(``int` `i = 0; i < k; i++) ` `        ``cur += arr[i]; ` ` `  `    ``// To store the minimum sum of k ` `    ``// consecutive elements of the array ` `    ``int` `min = cur; ` `    ``for` `(``int` `i = 0; i < n - k; i++) { ` ` `  `        ``// Calculating sum of next k elements ` `        ``cur = cur - arr[i] + arr[i + k]; ` ` `  `        ``// Update the minimum sum so far and the ` `        ``// index of the first element ` `        ``if` `(cur < min) { ` `            ``cur = min; ` `            ``index = i + 1; ` `        ``} ` `    ``} ` ` `  `    ``// Printing result ` `    ``for` `(``int` `i = 0; i < index; i++) ` `        ``cout << arr[i] << ``" "``; ` `    ``for` `(``int` `i = index + k; i < n; i++) ` `        ``cout << arr[i] << ``" "``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { -1, 1, 2, -3, 2, 2 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `k = 3; ` ` `  `    ``maxSumArr(arr, n, k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG { ` ` `  `    ``// Function to print the array after removing ` `    ``// k consecutive elements such that the sum ` `    ``// of the remaining elements is maximized ` `    ``static` `void` `maxSumArr(``int` `arr[], ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `cur = ``0``, index = ``0``; ` ` `  `        ``// Find the sum of first k elements ` `        ``for` `(``int` `i = ``0``; i < k; i++) ` `            ``cur += arr[i]; ` ` `  `        ``// To store the minimum sum of k ` `        ``// consecutive elements of the array ` `        ``int` `min = cur; ` `        ``for` `(``int` `i = ``0``; i < n - k; i++) { ` ` `  `            ``// Calculating sum of next k elements ` `            ``cur = cur - arr[i] + arr[i + k]; ` ` `  `            ``// Update the minimum sum so far and the ` `            ``// index of the first element ` `            ``if` `(cur < min) { ` `                ``cur = min; ` `                ``index = i + ``1``; ` `            ``} ` `        ``} ` ` `  `        ``// Printing result ` `        ``for` `(``int` `i = ``0``; i < index; i++) ` `            ``System.out.print(arr[i] + ``" "``); ` `        ``for` `(``int` `i = index + k; i < n; i++) ` `            ``System.out.print(arr[i] + ``" "``); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `arr[] = { -``1``, ``1``, ``2``, -``3``, ``2``, ``2` `}; ` `        ``int` `n = arr.length; ` `        ``int` `k = ``3``; ` ` `  `        ``maxSumArr(arr, n, k); ` `    ``} ` `} `

## Python

 `# Pyhton3 implementation of the approach ` ` `  `# Function to print the array after removing  ` `# k consecutive elements such that the sum  ` `# of the remaining elements is maximized ` `def` `maxSumArr(arr,  n, k): ` `    ``cur ``=` `0` `    ``index ``=` `0` ` `  `    ``# Find the sum of first k elements ` `    ``for` `i ``in` `range``(k): ` `        ``cur ``+``=` `arr[i] ` ` `  `    ``# To store the minimum sum of k  ` `    ``# consecutive elements of the array ` `    ``min` `=` `cur; ` `    ``for` `i ``in` `range``(n``-``k): ` ` `  `        ``# Calculating sum of next k elements  ` `        ``cur ``=` `cur``-``arr[i]``+``arr[i ``+` `k] ` `         `  `        ``# Update the minimum sum so far and the  ` `        ``# index of the first element ` `        ``if``(cur<``min``): ` `            ``cur ``=` `min` `            ``index ``=` `i ``+` `1` ` `  `    ``# Printing result ` `    ``for` `i ``in` `range``(index): ` `        ``print``(arr[i], end ``=``" "``) ` `    ``i ``=` `index ``+` `k ` `    ``while` `i

## C#

 `// C# implementation of the above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Function to print the array after removing ` `    ``// k consecutive elements such that the sum ` `    ``// of the remaining elements is maximized ` `    ``static` `void` `maxSumArr(``int` `[]arr, ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `cur = 0, index = 0; ` ` `  `        ``// Find the sum of first k elements ` `        ``for` `(``int` `i = 0; i < k; i++) ` `            ``cur = cur + arr[i]; ` ` `  `        ``// To store the minimum sum of k ` `        ``// consecutive elements of the array ` `        ``int` `min = cur; ` `        ``for` `(``int` `i = 0; i < n - k; i++) ` `        ``{ ` ` `  `            ``// Calculating sum of next k elements ` `            ``cur = (cur - arr[i]) + (arr[i + k]); ` ` `  `            ``// Update the minimum sum so far and the ` `            ``// index of the first element ` `            ``if` `(cur < min)  ` `            ``{ ` `                ``cur = min; ` `                ``index = i + 1; ` `            ``} ` `        ``} ` ` `  `        ``// Printing result ` `        ``for` `(``int` `i = 0; i < index; i++) ` `            ``Console.Write(arr[i] + ``" "``); ` `        ``for` `(``int` `i = index + k; i < n; i++) ` `            ``Console.Write(arr[i] + ``" "``); ` `    ``} ` ` `  `    ``// Driver code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``int` `[]arr = { -1, 1, 2, -3, 2, 2 }; ` `        ``int` `n = arr.Length; ` `        ``int` `k = 3; ` ` `  `        ``maxSumArr(arr, n, k); ` `    ``} ` `} ` ` `  `// This code is contributed by ajit..  `

Output:

```-1 2 2
```

Time Complexity: O(n)

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : jit_t

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.