- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Maximum Number of Events That Can Be Attended in C++

Suppose we have an array of events where events[i] = [startDayi, endDayi]. Here every event I start at startDayi and ends at endDayi. We can attend an event I at any day d where d in range startTimei and endTimei (both inclusive). We have to keep in mind that we can only attend one event at any time. So find the maximum number of events we can attend. So for example, if the input is like [[1,4], [4,4], [2,2], [3,4], [1,1]], then the output will be 1, as we can attend maximum of four events, these are [1, 1], [2, 2], [3, 4] and [4, 4].

To solve this, we will follow these steps −

n := number of events, then sort the event list based on the start date, set ret := 0 and itr := 0

create a priority queue based on max-heap called pq

for I in range 1 to 10000

while itr < n and events[itr, 0] = i

insert events[itr, 1]

increase itr by 1

while pq is not empty and top of pq < i,

delete element from pq

if pq is not empty, then delete from pq and increase ret by 1

return ret

## Example (C++)

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; class Solution { public: static bool cmp(vector <int>& a, vector <int>& b){ return a[0] < b[0]; } int maxEvents(vector<vector<int>>& events) { int n = events.size(); sort(events.begin(), events.end(), cmp); int ret = 0; int itr = 0; priority_queue <int, vector <int>, greater <int>> pq; for(int i = 1; i <= 1e5; i++){ while(itr < n && events[itr][0] == i){ pq.push(events[itr][1]); itr++; } while(!pq.empty() && pq.top() < i) pq.pop(); if(!pq.empty()){ pq.pop(); ret++; } } return ret; } }; main(){ vector<vector<int>> v = {{1,4},{4,4},{2,2},{3,4},{1,1}}; Solution ob; cout << (ob.maxEvents(v)); }

## Input

[[1,4],[4,4],[2,2],[3,4],[1,1]]

## Output

4

- Related Questions & Answers
- Maximum number of candies that can be bought in C
- Maximum number of people that can be killed with strength P in C++
- Maximum number of threads that can be created within a process in C
- Find maximum number that can be formed using digits of a given number in C++
- Maximum number of partitions that can be sorted individually to make sorted in C++
- C++ program to find out the maximum number of cells that can be illuminated
- Program to find maximum number of package that can be bought by buyers in C++
- Problem to Find Out the Maximum Number of Coins that Can be Collected in Python
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- Maximum number that can be display on Seven Segment Display using N segments in C++
- Maximum money that can be withdrawn in two steps in C
- Maximum litres of water that can be bought with N Rupees in C++
- Maximum number of 2×2 squares that can be fit inside a right isosceles triangle in C
- Maximum number of segments that can contain the given points in C++
- Set the maximum height that a box can be with CSS