Maximum number of overlapping Intervals | C ++ program that print maximum number of overlap among given ranges ; Function that print maximum overlap among ranges ; variable to store the maximum count ; storing the x and y coordinates in data vector ; pushing the x coordinate ; pushing the y coordinate ; sorting of ranges ; Traverse the data vector to count number of overlaps ; if x occur it means a new range is added so we increase count ; if y occur it means a range is ended so we decrease count ; updating the value of ans after every traversal ; printing the maximum value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void overlap ( vector < pair < int , int > > v ) { int ans = 0 ; int count = 0 ; vector < pair < int , char > > data ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { data . push_back ( { v [ i ] . first , ' x ' } ) ; data . push_back ( { v [ i ] . second , ' y ' } ) ; } sort ( data . begin ( ) , data . end ( ) ) ; for ( int i = 0 ; i < data . size ( ) ; i ++ ) { if ( data [ i ] . second == ' x ' ) count ++ ; if ( data [ i ] . second == ' y ' ) count -- ; ans = max ( ans , count ) ; } cout << ans << endl ; } int main ( ) { vector < pair < int , int > > v = { { 1 , 2 } , { 2 , 4 } , { 3 , 6 } } ; overlap ( v ) ; return 0 ; }