Given a sorted array **arr[]** of length **N** and an integer **K** such that **K < N**, the task is to remove exactly **K** elements from the array such that the sum of the differences of the consecutive elements of the array is maximized.

**Examples:**

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

Output:3

Let’s consider all the possible cases:

a) Remove arr[0]: arr[] = {2, 3, 4}, ans = 2

b) Remove arr[1]: arr[] = {1, 3, 4}, ans = 3

c) Remove arr[2]: arr[] = {1, 2, 4}, ans = 3

d) Remove arr[3]: arr[] = {1, 2, 3}, ans = 2

3 is the maximum of all the answers.

Input:arr[] = {1, 2, 10}, K = 2

Output:0

**Approach:** There are two cases:

- If
**K < N – 1**then the answer will be**arr[N – 1] – arr[0]**. This is because any K elements from the N – 2 internal elements of the array can be deleted without affecting the maximized sum of differences. For example, if any single element has to be removed from 1, 2, 3 and 4 then no matter whether 2 is removed or 3 is removed the final sum of difference will remain the same i.e. ((3 – 1) + (4 – 3)) = 3 which is equal to ((2 – 1) + (4 – 2)) = 3. - If
**K = N – 1**then the answer will be**0**because only a single element remains that is both the minimum and the maximum. Thus, the answer is**0**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the maximized sum ` `int` `findSum(` `int` `* arr, ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Remove any k internal elements ` ` ` `if` `(k <= n - 2) ` ` ` `return` `(arr[n - 1] - arr[0]); ` ` ` ` ` `return` `0; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 3, 4 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` `int` `k = 1; ` ` ` ` ` `cout << findSum(arr, n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the maximized sum ` ` ` `static` `int` `findSum(` `int` `[]arr, ` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` ` ` `// Remove any k internal elements ` ` ` `if` `(k <= n - ` `2` `) ` ` ` `return` `(arr[n - ` `1` `] - arr[` `0` `]); ` ` ` ` ` `return` `0` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `}; ` ` ` `int` `n = arr.length; ` ` ` `int` `k = ` `1` `; ` ` ` ` ` `System.out.println(findSum(arr, n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the maximized sum ` `def` `findSum(arr, n, k) : ` ` ` ` ` `# Remove any k internal elements ` ` ` `if` `(k <` `=` `n ` `-` `2` `) : ` ` ` `return` `(arr[n ` `-` `1` `] ` `-` `arr[` `0` `]); ` ` ` ` ` `return` `0` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `3` `, ` `4` `]; ` ` ` `n ` `=` `len` `(arr); ` ` ` `k ` `=` `1` `; ` ` ` ` ` `print` `(findSum(arr, n, k)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the maximized sum ` ` ` `static` `int` `findSum(` `int` `[]arr, ` ` ` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` ` ` `// Remove any k internal elements ` ` ` `if` `(k <= n - 2) ` ` ` `return` `(arr[n - 1] - arr[0]); ` ` ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `[]arr = { 1, 2, 3, 4 }; ` ` ` `int` `n = arr.Length; ` ` ` `int` `k = 1; ` ` ` ` ` `Console.WriteLine(findSum(arr, n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimize the sum of differences of consecutive elements after removing exactly K elements
- Maximize sum of consecutive differences in a circular array
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Maximize the maximum subarray sum after removing atmost one element
- Sequence with sum K and minimum sum of absolute differences between consecutive elements
- Maximize the sum of sum of the Array by removing end elements
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Maximize distance between any two consecutive 1's after flipping M 0's
- Sum of consecutive bit differences of first N non-negative integers
- Maximize cost to empty an array by removing contiguous subarrays of equal elements
- Average of remaining elements after removing K largest and K smallest elements from array
- Maximum distinct elements after removing k elements
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Minimum number of distinct elements after removing M items | Set 2
- First element of every K sets having consecutive elements with exactly K prime factors less than N
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Maximize product of digit sum of consecutive pairs in a subsequence of length K
- Maximum array sum that can be obtained after exactly k changes
- Maximize the size of array by deleting exactly k sub-arrays to make array prime

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.