Given an array of integer intervals [L, R] on the X axis, an overlap is considered when there is a non-zero interval shared between them.
Find the maximum number of intervals such that these intervals all overlap at some interval.
Examples:
[[1, 2], [2, 4]]
Return 0
Since there is no overlap among the interval
-------------
[[1, 2], [2, 4], [3, 5]]
Return 2
-------------
[[1, 3], [2, 4], [3, 5]]
Return 2
-------------
[[1, 3], [2, 4], [2, 5]]
Return 3
// C++ program that print maximum// number of overlap// among given ranges#include<bits/stdc++.h>usingnamespace std;// Function that print maximum// overlap among rangesvoidoverlap(vector<pair<int,int> > v){ // variable to store the maximum // countint ans =0;int count =0; vector<pair<int,char>> data; // storing the x and y // coordinates in data vectorfor (int i =0; i <v.size(); i++) { // pushing the x coordinatedata.push_back({ v[i].first,'x' }); // pushing the y coordinatedata.push_back({ v[i].second,'y' }); } // sorting of rangessort(data.begin(),data.end()); // Traverse the data vector to // count number of overlapsfor (int i =0; i <data.size(); i++) { // if x occur it means a new range // is added so we increase countif (data[i].second =='x') count++; // if y occur it means a range // is ended so we decrease countif (data[i].second =='y') count--; // updating the value of ans // after every traversal ans =max(ans, count); } // printing the maximum value cout << ans << endl;}// Driver codeintmain(){ vector<pair<int,int>> v= { { 1,2 }, { 2,4 } };overlap(v);return0;}