Maximums from array when the maximum decrements after every access

Given an integer K and an array of integers arr, the task is to find the maximum element from the array and after every retrieval the number will get decremented by 1. Repeat these steps exactly K number of times and print the sum of all the values retrieved in the end.

Examples:

Input: K = 3, arr[] = {2, 3, 5, 4}
Output: 13
For K = 1, current maximum is 5 (Sum = 5 and arr[] = {2, 3, 4, 4})
For K = 2, current maximum is 4 (Sum = 5 + 4 = 9 and arr[] = {2, 3, 3, 4})
For K = 3, current maximum is 4 (Sum = 9 + 4 = 13 and arr[] = {2, 3, 3, 3})
Hence, the result is 13

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

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

Approach: The main idea is to use a max heap which will have the maximum element at it’s root at any instance of time.

• Create a max heap of all the elements of the array.
• Get the root element of the heap and add it to the sum.
• Pop the root element and decrement it by 1 then insert it again into the heap.
• Repeat the above two steps exactly K number of times.
• Print the total sum in the end.

Below is the implementation of the above approach:

C++

 // C++ implementation of above approach #include using namespace std; #define ll long long    ll getSum(int arr[], int K, int n) {     ll sum = 0;     priority_queue maxHeap;     for (ll i = 0; i < n; i++) {            // put all array elements         // in a max heap         maxHeap.push(arr[i]);     }        while (K--) {            // Get the current maximum element         ll currentMax = maxHeap.top();            // Add it to the sum         sum += currentMax;            // Remove the current max from the heap         maxHeap.pop();            // Add the current max back to the         // heap after decrementing it by 1         maxHeap.push(currentMax - 1);     }     return sum; }    // driver code int main() {     int arr[] = { 2, 3, 5, 4 }, K = 3;     int n = sizeof(arr) / sizeof(arr);     cout << getSum(arr, K, n) << endl; }

Java

 // Java implementation of above approach  import java.util.*; class Solution {    static int getSum(int arr[], int K, int n)  {      int sum = 0;      PriorityQueue maxHeap =                            new PriorityQueue(n,Collections.reverseOrder());      for (int i = 0; i < n; i++) {             // put aint array elements          // in a max heap          maxHeap.add(arr[i]);      }         while (K-->0) {             // Get the current maximum element          int currentMax = (int)maxHeap.peek();             // Add it to the sum          sum += currentMax;            // Remove the current max from the heap          maxHeap.remove();             // Add the current max back to the          // heap after decrementing it by 1          maxHeap.add(currentMax - 1);      }      return sum;  }     // driver code  public static void main(String args[])  {      int arr[] = { 2, 3, 5, 4 }, K = 3;      int n =arr.length;      System.out.println(getSum(arr, K, n));  }  } //cntributed by Arnab Kundu

Output:

13

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 : andrew1234

Article Tags :
Practice Tags :

Be the First to upvote.

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