Ticket sellers | Practice | GeeksforGeeks

There are N ticket sellers where the ith ticket seller has A[i] tickets. 
The price of a ticket is the number of tickets remaining with the ticket seller. They are allowed to sell at most K tickets. Find the maximum amount they can earn by selling K tickets. 
The amount of tickets of each seller is provided in array A. Give the answer modulo 109 + 7.
Example 1:
Input: N = 5, K = 3
A = {4, 3, 6, 2, 4}
Output: 15
Explaination: Consider 0 based indexing. 
For first two turns the 2nd seller sells. 
For the third turn either 0th or 2nd 
seller can sell. So the total becomes 
6 + 5 + 4 = 15.
Example 2:
Input: N = 6, K = 2
A = {5, 3, 5, 2, 4, 4}
Output: 10
Explaination: The turns are taken by 
0th and 2nd seller. 5 + 5 = 10 is the 
maximum amount.
Your Task:
You do not need to take input or print anything. Your task is to complete the function maxAmount() which takes N, K, and the array A as input parameters and returns the maximum amount they can get by selling K tickets.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 105
1 ≤ A[i], K ≤ 106
-1
chessnoobdj4 months ago
C++
int maxAmount(int N, int K, int A[])
    {
        int ans = 0, mod = 1e9+7;
        priority_queue <int> pq;
        for(int i=0; i<N; i++)
            pq.push(A[i]);
        while(K--){
            int num = pq.top();
            pq.pop();
            ans += num;
            pq.push(num-1);
            ans %= mod;
        }
        return ans;
    }
0
pratikpatil56116 months ago
Error In Driver Code
0
chiragjaiswal4547 months ago
Expected Time Complexity Should be : O(NlogN)
0
Chirag Jaiswal7 months ago
Chirag Jaiswal
All solution are of O(NlogN) or O(NlogK) time complexity...Is there any solution exist with O(N) ????
0
Chirag Jaiswal
This comment was deleted.
0
Chirag Jaiswal
This comment was deleted.
0
Sk Jishan Ali7 months ago
Sk Jishan Ali
please update the driver code in java as the array 'A' is initialized as int A=new int[N];
0
Tushar Saini8 months ago
Tushar Saini
void heapify(int arr[],int index,int n){    int left=2*index+1;    int right=2*index+2;    int largest=index;    if(left<n &&="" arr[largest]<arr[left])="" largest="left;" if(right<n="" &&="" arr[largest]<arr[right])="" largest="right;" if(largest!="index)" {="" swap(arr[largest],arr[index]);="" heapify(arr,largest,n);="" }="" }="" void="" buildheap(int="" arr[],int="" n)="" {="" for(int="" i="n/2" -1;i="">=0;i--)    heapify(arr,i,n);}class Solution{public:    int maxAmount(int n, int k, int arr[])    {      int count=0;//track of count      int mx=0;//number totla      buildheap(arr,n);      while(count<k) {="" mx="(mx%1000000007" +="" arr[0]%1000000007)%1000000007;="" arr[0]--;="" heapify(arr,0,n);="" count++;="" }="" o(n)+o(kl0ogn="" return="" mx;="" }="" };="">
0
VISHAL SHARMA8 months ago
VISHAL SHARMA
https://uploads.disquscdn.c...
Using Priority queue
0
navidulhaque10 months ago
navidulhaque
time taken 0.95sec---int maxAmount(int N, int K, int A[]) {  // code here  vector < int > v;  int max1 = 0;  int curr = 0;  for (int i = 0; i < N; i++) {    v.push_back(A[i]);  }  make_heap(v.begin(), v.end());  while (K--) {    curr = v.front();    pop_heap(v.begin(), v.end());    v.pop_back();    max1 += curr % 1000000007;    v.push_back((curr - 1));    push_heap(v.begin(), v.end());  }  return max1 % 1000000007;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.