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 = 3Output: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 = 6Output: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;` `}` |

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

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

## C#

`// C# implementation of the approach` `using` `System;` `using` `System.Collections.Generic;` `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` ` ` `List<` `int` `> pq = ` `new` `List<` `int` `>();` ` ` `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` ` ` `pq.Sort();` ` ` `pq.Reverse();` ` ` `int` `top = pq[0];` ` ` ` ` `// Remove it from the queue` ` ` `pq.RemoveAt(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.Add(top - 1);` ` ` ` ` `// Update the count of persons` ` ` `c++;` ` ` `}` ` ` `return` `profit;` `}` `// Driver Code` `static` `void` `Main()` `{` ` ` `int` `[] seats = { 2, 3, 4, 5, 1 };` ` ` `int` `k = seats.Length;` ` ` `int` `n = 6;` ` ` ` ` `Console.Write(maxProfit(seats, k, n));` `}` `}` `// This code is contributed by divyeshrabadiya07` |

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