Related Articles

# Maximum number of diamonds that can be gained in K minutes

• Difficulty Level : Medium
• Last Updated : 29 Jun, 2021

Given an array arr[] consisting of N positive integers such that arr[i] represents that the ith bag contains arr[i] diamonds and a positive integer K, the task is to find the maximum number of diamonds that can be gained in exactly K minutes if dropping a bag takes 1 minute such that if a bag with P diamonds is dropped, then it changes to [P/2] diamonds, and P diamonds are gained.

Examples:

Input: arr[] = {2, 1, 7, 4, 2}, K = 3
Output: 14
Explanation:
The initial state of bags is {2, 1, 7, 4, 2}.
Operation 1: Take all diamonds from third bag i.e., arr(= 7), the state of bags becomes: {2, 1, 3, 4, 2}.
Operation 2: Take all diamonds from fourth bag i.e., arr(= 4), the state of bags becomes: {2, 1, 3, 2, 2}.
Operation 3: Take all diamonds from Third bag i.e., arr(= 3), the state of bags becomes{2, 1, 1, 2, 2}.
Therefore, the total diamonds gains is 7 + 4 + 3 = 14.

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

Approach: The given problem can be solved by using the Greedy Approach with the help of max-heap. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the maximum number``// of diamonds that can be gained in``// exactly K minutes``void` `maxDiamonds(``int` `A[], ``int` `N, ``int` `K)``{``    ``// Stores all the array elements``    ``priority_queue<``int``> pq;` `    ``// Push all the elements to the``    ``// priority queue``    ``for` `(``int` `i = 0; i < N; i++) {``        ``pq.push(A[i]);``    ``}` `    ``// Stores the required result``    ``int` `ans = 0;` `    ``// Loop while the queue is not``    ``// empty and K is positive``    ``while` `(!pq.empty() && K--) {` `        ``// Store the top element``        ``// from the pq``        ``int` `top = pq.top();` `        ``// Pop it from the pq``        ``pq.pop();` `        ``// Add it to the answer``        ``ans += top;` `        ``// Divide it by 2 and push it``        ``// back to the pq``        ``top = top / 2;``        ``pq.push(top);``    ``}` `    ``// Print the answer``    ``cout << ans;``}` `// Driver Code``int` `main()``{``    ``int` `A[] = { 2, 1, 7, 4, 2 };``    ``int` `K = 3;``    ``int` `N = ``sizeof``(A) / ``sizeof``(A);``    ``maxDiamonds(A, N, K);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to find the maximum number``// of diamonds that can be gained in``// exactly K minutes``static` `void` `maxDiamonds(``int` `A[], ``int` `N, ``int` `K)``{``    ` `    ``// Stores all the array elements``    ``PriorityQueue pq = ``new` `PriorityQueue<>(``        ``(a, b) -> b - a);` `    ``// Push all the elements to the``    ``// priority queue``    ``for``(``int` `i = ``0``; i < N; i++)``    ``{``        ``pq.add(A[i]);``    ``}` `    ``// Stores the required result``    ``int` `ans = ``0``;` `    ``// Loop while the queue is not``    ``// empty and K is positive``    ``while` `(!pq.isEmpty() && K-- > ``0``)``    ``{``        ` `        ``// Store the top element``        ``// from the pq``        ``int` `top = pq.peek();` `        ``// Pop it from the pq``        ``pq.remove();` `        ``// Add it to the answer``        ``ans += top;` `        ``// Divide it by 2 and push it``        ``// back to the pq``        ``top = top / ``2``;``        ``pq.add(top);``    ``}` `    ``// Print the answer``    ``System.out.print(ans);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `A[] = { ``2``, ``1``, ``7``, ``4``, ``2` `};``    ``int` `K = ``3``;``    ``int` `N = A.length;``    ` `    ``maxDiamonds(A, N, K);``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 program for the above approach` `# Function to find the maximum number``# of diamonds that can be gained in``# exactly K minutes``def` `maxDiamonds(A, N, K):``    ` `    ``# Stores all the array elements``    ``pq ``=` `[]` `    ``# Push all the elements to the``    ``# priority queue``    ``for` `i ``in` `range``(N):``        ``pq.append(A[i])``        ` `    ``pq.sort()` `    ``# Stores the required result``    ``ans ``=` `0` `    ``# Loop while the queue is not``    ``# empty and K is positive``    ``while` `(``len``(pq) > ``0` `and` `K > ``0``):``        ``pq.sort()``        ` `        ``# Store the top element``        ``# from the pq``        ``top ``=` `pq[``len``(pq) ``-` `1``]` `        ``# Pop it from the pq``        ``pq ``=` `pq[``0``:``len``(pq) ``-` `1``]` `        ``# Add it to the answer``        ``ans ``+``=` `top` `        ``# Divide it by 2 and push it``        ``# back to the pq``        ``top ``=` `top ``/``/` `2``;``        ``pq.append(top)``        ``K ``-``=` `1` `    ``# Print the answer``    ``print``(ans)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``A ``=` `[ ``2``, ``1``, ``7``, ``4``, ``2` `]``    ``K ``=` `3``    ``N ``=` `len``(A)``    ` `    ``maxDiamonds(A, N, K)` `# This code is contributed by SURENDRA_GANGWAR`

## Javascript

 ``
Output:
`14`

Time Complexity: O((N + K)*log N)
Auxiliary Space: O(N)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up