# Maximize the profit after selling the tickets

Given an array seats[] where seat[i] is the number of vacant seats in the ith row in a stadium for a cricket match. There are N people in a queue waiting to buy the tickets. Each seat costs equal to the number of vacant seats in the row it belongs to. The task is to maximize the profit by selling the tickets to N people.

Examples:

Input: seats[] = {2, 1, 1}, N = 3
Output: 4
Person 1: Sell the seat in the row with
2 vacant seats, seats = {1, 1, 1}
Person 2: All the rows have 1 vacant
seat each, seats[] = {0, 1, 1}
Person 3: seats[] = {0, 0, 1}

Input: seats[] = {2, 3, 4, 5, 1}, N = 6
Output: 22

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

Approach: In order to maximize the profit, the ticket must be for the seat in a row which has the maximum number of vacant seats and the number of vacant seats in that row will be decrement by 1 as one of the seats has just been sold. All the persons can be sold a seat ticket until there are vacant seats. This can be computed efficiently with the help of a priority_queue.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the maximized profit ` `int` `maxProfit(``int` `seats[], ``int` `k, ``int` `n) ` `{ ` ` `  `    ``// Push all the vacant seats ` `    ``// in a priority queue ` `    ``priority_queue<``int``> pq; ` `    ``for` `(``int` `i = 0; i < k; i++) ` `        ``pq.push(seats[i]); ` ` `  `    ``// To store the maximized profit ` `    ``int` `profit = 0; ` ` `  `    ``// To count the people that ` `    ``// have been sold a ticket ` `    ``int` `c = 0; ` `    ``while` `(c < n) { ` ` `  `        ``// Get the maximimum number of ` `        ``// vacant seats for any row ` `        ``int` `top = pq.top(); ` ` `  `        ``// Remove it from the queue ` `        ``pq.pop(); ` ` `  `        ``// If there are no vacant seats ` `        ``if` `(top == 0) ` `            ``break``; ` ` `  `        ``// Update the profit ` `        ``profit = profit + top; ` ` `  `        ``// Push the updated status of the ` `        ``// vacant seats in the current row ` `        ``pq.push(top - 1); ` ` `  `        ``// Update the count of persons ` `        ``c++; ` `    ``} ` `    ``return` `profit; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `seats[] = { 2, 3, 4, 5, 1 }; ` `    ``int` `k = ``sizeof``(seats) / ``sizeof``(``int``); ` `    ``int` `n = 6; ` ` `  `    ``cout << maxProfit(seats, k, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `// Function to return the maximized profit  ` `static` `int` `maxProfit(``int` `seats[], ``int` `k, ``int` `n)  ` `{  ` `     `  `    ``// Push all the vacant seats  ` `    ``// in a priority queue  ` `    ``PriorityQueue pq; ` `    ``pq = ``new` `PriorityQueue<>(Collections.reverseOrder()); ` `     `  `    ``for``(``int` `i = ``0``; i < k; i++)  ` `       ``pq.add(seats[i]);  ` ` `  `    ``// To store the maximized profit  ` `    ``int` `profit = ``0``;  ` ` `  `    ``// To count the people that  ` `    ``// have been sold a ticket  ` `    ``int` `c = ``0``;  ` `    ``while` `(c < n) ` `    ``{  ` ` `  `        ``// Get the maximimum number of  ` `        ``// vacant seats for any row  ` `        ``int` `top = pq.remove();  ` ` `  `        ``// If there are no vacant seats  ` `        ``if` `(top == ``0``)  ` `            ``break``;  ` ` `  `        ``// Update the profit  ` `        ``profit = profit + top;  ` ` `  `        ``// Push the updated status of the  ` `        ``// vacant seats in the current row  ` `        ``pq.add(top - ``1``);  ` ` `  `        ``// Update the count of persons  ` `        ``c++;  ` `    ``}  ` `     `  `    ``return` `profit;  ` `}  ` `     `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `seats[] = { ``2``, ``3``, ``4``, ``5``, ``1` `};  ` `    ``int` `k = seats.length; ` `    ``int` `n = ``6``; ` ` `  `    ``System.out.println(maxProfit(seats, k ,n)); ` `} ` `} ` ` `  `// This code is contributed by rutvik_56 `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to return the maximized profit  ` `def` `maxProfit(seats, k, n) :  ` ` `  `    ``# Push all the vacant seats  ` `    ``# in a priority queue  ` `    ``pq ``=` `[];  ` `    ``for` `i ``in` `range``(k) : ` `        ``pq.append(seats[i]); ` `     `  `    ``# for maintaining the property of max heap ` `    ``pq.sort(reverse ``=` `True``); ` `     `  `    ``# To store the maximized profit  ` `    ``profit ``=` `0``;  ` ` `  `    ``# To count the people that  ` `    ``# have been sold a ticket  ` `    ``c ``=` `0``;  ` `    ``while` `(c < n) : ` `         `  `        ``# for maintaining the property of max heap ` `        ``pq.sort(reverse ``=` `True``); ` `         `  `        ``# Get the maximimum number of  ` `        ``# vacant seats for any row  ` `        ``top ``=` `pq[``0``]; ` `         `  `        ``# Remove it from the queue ` `        ``pq.pop(``0``); ` `         `  `        ``# If there are no vacant seats ` `        ``if` `(top ``=``=` `0``) : ` `            ``break``; ` `             `  `        ``# Update the profit ` `        ``profit ``=` `profit ``+` `top; ` `         `  `        ``# Push the updated status of the ` `        ``# vacant seats in the current row ` `        ``pq.append(top ``-` `1``); ` `         `  `        ``# Update the count of persons ` `        ``c ``+``=` `1``;  ` `     `  `    ``return` `profit;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``seats ``=` `[ ``2``, ``3``, ``4``, ``5``, ``1` `];  ` `    ``k ``=` `len``(seats);  ` `    ``n ``=` `6``;  ` ` `  `    ``print``(maxProfit(seats, k, n));  ` ` `  `# This code is contributed by AnkitRai01 `

Output:

```22
```

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.

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 : AnkitRai01, rutvik_56

Article Tags :

4

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