Maximum number of overlapping Intervals
Given different intervals, the task is to print the maximum number of overlap among these intervals at any time.
Examples:
Input: v = {{1, 2}, {2, 4}, {3, 6}}
Output: 2
The maximum overlapping is 2(between (1 2) and (2 4) or between (2 4) and (3 6))
Input: v = {{1, 8}, {2, 5}, {5, 6}, {3, 7}}
Output: 4
The maximum overlapping is 4 (between (1, 8), (2, 5), (5, 6) and (3, 7))
Approach:
- The idea is to store coordinates in a new vector of pair mapped with characters ‘x’ and ‘y’, to identify coordinates.
- Sort the vector.
- Traverse the vector, if an x coordinate is encountered it means a new range is added, so update count and if y coordinate is encountered that means a range is subtracted.
- Update the value of count for every new coordinate and take maximum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
static class pair
{
int first;
char second;
pair( int first, char second)
{
this .first = first;
this .second = second;
}
}
static void overlap( int [][] v)
{
int ans = 0 ;
int count = 0 ;
ArrayList<pair> data = new ArrayList<>();
for ( int i = 0 ; i < v.length; i++)
{
data.add( new pair(v[i][ 0 ], 'x' ));
data.add( new pair(v[i][ 1 ], 'y' ));
}
Collections.sort(data, (a, b) -> a.first - b.first);
for ( int i = 0 ; i < data.size(); i++)
{
if (data.get(i).second == 'x' )
count++;
if (data.get(i).second == 'y' )
count--;
ans = Math.max(ans, count);
}
System.out.println(ans);
}
public static void main(String[] args)
{
int [][] v = { { 1 , 2 },
{ 2 , 4 },
{ 3 , 6 } };
overlap(v);
}
}
|
Python3
def overlap(v):
ans = 0
count = 0
data = []
for i in range ( len (v)):
data.append([v[i][ 0 ], 'x' ])
data.append([v[i][ 1 ], 'y' ])
data = sorted (data)
for i in range ( len (data)):
if (data[i][ 1 ] = = 'x' ):
count + = 1
if (data[i][ 1 ] = = 'y' ):
count - = 1
ans = max (ans, count)
print (ans)
v = [ [ 1 , 2 ], [ 2 , 4 ], [ 3 , 6 ] ]
overlap(v)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
class pair
{
public int first;
public char second;
public pair( int first, char second)
{
this .first = first;
this .second = second;
}
}
public static void overlap( int [,] v)
{
var ans = 0;
var count = 0;
var data = new List<pair>();
for ( int i = 0; i < v.GetLength(0); i++)
{
data.Add( new pair(v[i,0], 'x' ));
data.Add( new pair(v[i,1], 'y' ));
}
data.Sort((a,b)=>a.first - b.first);
for ( int i = 0; i < data.Count; i++)
{
if (data[i].second == 'x' )
{
count++;
}
if (data[i].second == 'y' )
{
count--;
}
ans = Math.Max(ans,count);
}
Console.WriteLine(ans);
}
public static void Main(String[] args)
{
int [,] v = {{1, 2}, {2, 4}, {3, 6}};
GFG.overlap(v);
}
}
|
Javascript
<script>
function overlap(v)
{
var ans = 0;
var count = 0;
var data = [];
for ( var i = 0; i < v.length; i++)
{
data.push([v[i][0], 'x' ]);
data.push([v[i][1], 'y' ]);
}
data.sort();
for ( var i = 0; i < data.length; i++)
{
if (data[i][1] == 'x' )
count++;
if (data[i][1] == 'y' )
count--;
ans = Math.max(ans, count);
}
document.write(ans + "<br>" );
}
var v = [ [ 1, 2 ], [ 2, 4 ], [ 3, 6 ] ];
overlap(v);
</script>
|
Time Complexity: O(N log N), for sorting the data vector.
Auxiliary Space: O(N), for creating an additional array of size N.
Last Updated :
29 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...