Maximums from array when the maximum decrements after every access
Last Updated :
24 Apr, 2023
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
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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll getSum( int arr[], int K, int n)
{
ll sum = 0;
priority_queue<ll> maxHeap;
for (ll i = 0; i < n; i++) {
maxHeap.push(arr[i]);
}
while (K--) {
ll currentMax = maxHeap.top();
sum += currentMax;
maxHeap.pop();
maxHeap.push(currentMax - 1);
}
return sum;
}
int main()
{
int arr[] = { 2, 3, 5, 4 }, K = 3;
int n = sizeof (arr) / sizeof (arr[0]);
cout << getSum(arr, K, n) << endl;
}
|
Java
import java.util.*;
class Solution
{
static int getSum( int arr[], int K, int n)
{
int sum = 0 ;
PriorityQueue<Integer> maxHeap =
new PriorityQueue<Integer>(n,Collections.reverseOrder());
for ( int i = 0 ; i < n; i++) {
maxHeap.add(arr[i]);
}
while (K--> 0 ) {
int currentMax = ( int )maxHeap.peek();
sum += currentMax;
maxHeap.remove();
maxHeap.add(currentMax - 1 );
}
return sum;
}
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));
}
}
|
Python3
import heapq
def getSum(arr, K, n):
Sum = 0
maxHeap = arr
heapq._heapify_max(maxHeap)
while (K > 0 ):
currentMax = maxHeap[ 0 ]
Sum + = currentMax
maxHeap[ 0 ] - = 1
heapq._heapify_max(maxHeap)
K - = 1
return Sum
arr = [ 2 , 3 , 5 , 4 ]
K = 3
n = len (arr)
print (getSum(arr, K, n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int getSum( int [] arr, int K, int n)
{
int sum = 0;
List< int > maxHeap = new List< int >();
for ( int i = 0; i < n; i++) {
maxHeap.Add(arr[i]);
}
maxHeap.Sort();
maxHeap.Reverse();
while (K-- > 0) {
int currentMax = maxHeap[0];
sum += currentMax;
maxHeap.RemoveAt(0);
maxHeap.Add(currentMax - 1);
maxHeap.Sort();
maxHeap.Reverse();
}
return sum;
}
static void Main()
{
int [] arr = { 2, 3, 5, 4 };
int K = 3;
int n = arr.Length;
Console.Write(getSum(arr, K, n));
}
}
|
Javascript
<script>
function getSum(arr, K, n)
{
let sum = 0;
let maxHeap = [];
for (let i = 0; i < n; i++) {
maxHeap.push(arr[i]);
}
maxHeap.sort( function (a, b){ return a - b});
maxHeap.reverse();
while (K-- > 0) {
let currentMax = maxHeap[0];
sum += currentMax;
maxHeap.shift();
maxHeap.push(currentMax - 1);
maxHeap.sort( function (a, b){ return a - b});
maxHeap.reverse();
}
return sum;
}
let arr = [ 2, 3, 5, 4 ];
let K = 3;
let n = arr.length;
document.write(getSum(arr, K, n));
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...