Given an array arr of size N and a value K, the task is to empty this array by removing maximum of K from it. Print the value removed if the array was emptied, else -1.

**Examples:**

Input:arr[] = {2, 2}, K = 5

Output:4

Explanation:

After removing arr[1] (=2) from K (=5), K = 5 – 2 = 3

After removing arr[2] (=2) from K (=3), K = 3 – 2 = 1

Since the array has been emptied and K is still > 0

Therefore the value removed = 2 + 2 = 4

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

Output:-1

Explanation:

After removing arr[1] (=5) from K (=10), K = 10 – 5 = 5

After removing arr[2] (=3) from K (=5), K = 5 – 3 = 2

After removing arr[3] (=2) from K (=2), K = 2 – 2 = 0

After removing arr[4] (=2) from K (=0), K = 0 – 2 = -2

Since the array has been emptied and K is < 0

Therefore the array cannot be emptied for this K

hence the output is -1

**Approach:**

- The given problem can be observed as finding the total sum of the array arr and removing this sum from K.
- If this sum is less than or equal to K, then the array can be emptied and the output will be the sum of the array.
- Else the output will be -1.

Below is the implementation of the above approach:

## C++

`// C++ program to empty an Array ` `// by removing a maximum of K value from it ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to Empty an Array by removing ` `// maximum of K value from it ` `void` `emptyArray(` `long` `long` `int` `n, ` ` ` `long` `long` `int` `k, ` ` ` `long` `long` `int` `arr[]) ` `{ ` ` ` `long` `long` `int` `sum = 0, i; ` ` ` ` ` `// Find the total sum of the array ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `sum += arr[i]; ` ` ` ` ` `// Find if sum is less than or equal to K ` ` ` `if` `(sum <= k) ` ` ` `cout << sum << ` `"\n"` `; ` ` ` `else` ` ` `cout << -1 << ` `"\n"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `long` `long` `int` `n = 2, k = 5; ` ` ` `long` `long` `int` `arr[] = { 2, 2 }; ` ` ` ` ` `emptyArray(n, k, arr); ` ` ` ` ` `n = 4, k = 10; ` ` ` `long` `long` `int` `arr1[] = { 5, 3, 2, 2 }; ` ` ` ` ` `emptyArray(n, k, arr1); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to empty an Array ` `// by removing a maximum of K value from it ` `class` `GFG ` `{ ` ` ` ` ` `// Function to Empty an Array by removing ` ` ` `// maximum of K value from it ` ` ` `static` `void` `emptyArray(` `int` `n, ` `int` `k, ` `int` `arr[]) ` ` ` `{ ` ` ` `int` `sum = ` `0` `, i; ` ` ` ` ` `// Find the total sum of the array ` ` ` `for` `(i = ` `0` `; i < n; i++) ` ` ` `sum += arr[i]; ` ` ` ` ` `// Find if sum is less than or equal to K ` ` ` `if` `(sum <= k) ` ` ` `System.out.println(sum); ` ` ` `else` ` ` `System.out.println(-` `1` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `2` `; ` ` ` `int` `k = ` `5` `; ` ` ` `int` `arr[] = { ` `2` `, ` `2` `}; ` ` ` ` ` `emptyArray(n, k, arr); ` ` ` ` ` `n = ` `4` `; k = ` `10` `; ` ` ` `int` `arr1[] = { ` `5` `, ` `3` `, ` `2` `, ` `2` `}; ` ` ` ` ` `emptyArray(n, k, arr1); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to empty an Array ` `# by removing a maximum of K value from it ` ` ` `# Function to Empty an Array by removing ` `# maximum of K value from it ` `def` `emptyArray(n, k, arr) : ` ` ` ` ` `sum` `=` `0` `; ` ` ` ` ` `# Find the total sum of the array ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `sum` `+` `=` `arr[i]; ` ` ` ` ` `# Find if sum is less than or equal to K ` ` ` `if` `(` `sum` `<` `=` `k) : ` ` ` `print` `(` `sum` `); ` ` ` `else` `: ` ` ` `print` `(` `-` `1` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `2` `; k ` `=` `5` `; ` ` ` `arr ` `=` `[ ` `2` `, ` `2` `]; ` ` ` ` ` `emptyArray(n, k, arr); ` ` ` ` ` `n ` `=` `4` `; k ` `=` `10` `; ` ` ` `arr1 ` `=` `[ ` `5` `, ` `3` `, ` `2` `, ` `2` `]; ` ` ` ` ` `emptyArray(n, k, arr1); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to empty an Array ` `// by removing a maximum of K value from it ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to Empty an Array by removing ` ` ` `// maximum of K value from it ` ` ` `static` `void` `emptyArray(` `int` `n, ` `int` `k, ` `int` `[]arr) ` ` ` `{ ` ` ` `int` `sum = 0, i; ` ` ` ` ` `// Find the total sum of the array ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `sum += arr[i]; ` ` ` ` ` `// Find if sum is less than or equal to K ` ` ` `if` `(sum <= k) ` ` ` `Console.WriteLine(sum); ` ` ` `else` ` ` `Console.WriteLine(-1); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `n = 2; ` ` ` `int` `k = 5; ` ` ` `int` `[]arr = { 2, 2 }; ` ` ` ` ` `emptyArray(n, k, arr); ` ` ` ` ` `n = 4; k = 10; ` ` ` `int` `[]arr1 = { 5, 3, 2, 2 }; ` ` ` ` ` `emptyArray(n, k, arr1); ` ` ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

4 -1

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:

- Steps to make array empty by removing maximum and its right side
- Min steps to empty an Array by removing a pair each time with sum at most K
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Maximize cost to empty an array by removing contiguous subarrays of equal elements
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- Minimize cost to empty a given string by removing characters alphabetically
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Maximum length of Strictly Increasing Sub-array after removing at most one element
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Maximum sum subarray removing at most one element
- Maximum points covered after removing an Interval
- Maximize the maximum subarray sum after removing atmost one element
- Maximum contiguous decreasing sequence obtained by removing any one element
- Maximum distinct elements after removing k elements
- Sum of values of all possible non-empty subsets of the given array
- Product of values of all possible non-empty subsets of given Array
- Check if an array can be formed by merging 2 non-empty permutations
- Minimum palindromic subarray removals to make array Empty

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.