Maximum number of diamonds that can be gained in K minutes
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[2](= 7), the state of bags becomes: {2, 1, 3, 4, 2}.
Operation 2: Take all diamonds from fourth bag i.e., arr[3](= 4), the state of bags becomes: {2, 1, 3, 2, 2}.
Operation 3: Take all diamonds from Third bag i.e., arr[2](= 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++
#include <bits/stdc++.h>
using namespace std;
void maxDiamonds( int A[], int N, int K)
{
priority_queue< int > pq;
for ( int i = 0; i < N; i++) {
pq.push(A[i]);
}
int ans = 0;
while (!pq.empty() && K--) {
int top = pq.top();
pq.pop();
ans += top;
top = top / 2;
pq.push(top);
}
cout << ans;
}
int main()
{
int A[] = { 2, 1, 7, 4, 2 };
int K = 3;
int N = sizeof (A) / sizeof (A[0]);
maxDiamonds(A, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maxDiamonds( int A[], int N, int K)
{
PriorityQueue<Integer> pq = new PriorityQueue<>(
(a, b) -> b - a);
for ( int i = 0 ; i < N; i++)
{
pq.add(A[i]);
}
int ans = 0 ;
while (!pq.isEmpty() && K-- > 0 )
{
int top = pq.peek();
pq.remove();
ans += top;
top = top / 2 ;
pq.add(top);
}
System.out.print(ans);
}
public static void main(String[] args)
{
int A[] = { 2 , 1 , 7 , 4 , 2 };
int K = 3 ;
int N = A.length;
maxDiamonds(A, N, K);
}
}
|
Python3
def maxDiamonds(A, N, K):
pq = []
for i in range (N):
pq.append(A[i])
pq.sort()
ans = 0
while ( len (pq) > 0 and K > 0 ):
pq.sort()
top = pq[ len (pq) - 1 ]
pq = pq[ 0 : len (pq) - 1 ]
ans + = top
top = top / / 2 ;
pq.append(top)
K - = 1
print (ans)
if __name__ = = '__main__' :
A = [ 2 , 1 , 7 , 4 , 2 ]
K = 3
N = len (A)
maxDiamonds(A, N, K)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static void maxDiamonds( int []A, int N, int K)
{
var pq = new List< int >();
for ( int i = 0; i < N; i++)
{
pq.Add(A[i]);
}
int ans = 0;
while (pq.Count!=0 && K-- > 0)
{
pq.Sort();
int top = pq[pq.Count-1];
pq.RemoveAt(pq.Count-1);
ans += top;
top = top / 2;
pq.Add(top);
}
Console.WriteLine(ans);
}
public static void Main( string [] args)
{
int []A= { 2, 1, 7, 4, 2 };
int K = 3;
int N = A.Length;
maxDiamonds(A, N, K);
}
}
|
Javascript
<script>
function maxDiamonds(A, N, K) {
let pq = [];
for (let i = 0; i < N; i++) {
pq.push(A[i]);
}
let ans = 0;
pq.sort((a, b) => a - b)
while (pq.length && K--) {
pq.sort((a, b) => a - b)
let top = pq[pq.length - 1];
pq.pop();
ans += top;
top = Math.floor(top / 2);
pq.push(top);
}
document.write(ans);
}
let A = [2, 1, 7, 4, 2];
let K = 3;
let N = A.length;
maxDiamonds(A, N, K);
</script>
|
Time Complexity: O((N + K)*log N)
Auxiliary Space: O(N)
Last Updated :
28 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...