Given an array **seats[]** where **seat[i]** is the number of vacant seats in the **i ^{th}** 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

**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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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<Integer> 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Find the maximum amount that can be collected by selling movie tickets
- Maximize the profit by selling at-most M products
- Maximize profit in buying and selling stocks with Rest condition
- Maximum profit after buying and selling the stocks
- Maximum profit by selling N items at two markets
- Maximum profit by buying and selling a share at most K times | Greedy Approach
- Maximum profit by buying and selling a stock at most twice | Set 2
- Stock Buy Sell to Maximize Profit
- Maximize Profit by trading stocks based on given rate per day
- Calculate the loss incurred in selling the given items at discounted price
- Maximize array sum after K negations | Set 1
- Maximize array sum after K negations | Set 2
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximize the median of the given array after adding K elements to the same array
- Maximize the maximum subarray sum after removing atmost one element
- Maximize distance between any two consecutive 1's after flipping M 0's
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Maximize the sum of array after multiplying a prefix and suffix by -1
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Maximize difference between the Sum of the two halves of the Array after removal of N elements

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.