The call center company Cell Labs is doing yearly analysis of the business of their Mumbai office. They have taken a particular date and have extracted the call records of that day. The call records are in the form of two values, first the start time of the call and second the end time of the same call.
Given a call center executive can only attend to one call at a time, the company wants to find out what was the maximum number of call executives that were busy at the same time on the given date. You need to help them find that value.
Input Format
The first line of input contains a single integer n, which is the number of call records
The second line of input contains n space separated integers, sy, s …sn which are timestamps of start time of n respective calls (in the HHMM format like 0903, 1830)
The second line of input contains n space separated integers, e, e; …en which are timestamps of end time of n respective calls (in the HHMM format like 0910, 1840)
Constraints
1<=n<99999
si<ei for all 1<=i<=n
Output Format
A single integer that is the maximum number of call executives that were busy on calls at the same time on the given date.
Sample Input:
6
0630 0710 0720 0830 0845 1550
0640 0930 0910 0900 1050 1750
Sample Output:
4
Explanation
Consider the time between 0845 and 0900. There are 4 calls that were
ongoing during this time.
0710 to 0930
0720to 0910
0830 to 0900
0845 to 1050
This means at least 4 different call executives were busy taking these calls. This is also the maximum executives consumption for all calls.
Hence the answer 4.
This means at least 4 different call executives were busy taking these calls. This is also the maximum executives consumption for all calls.
Hence the answer 4.
Sample Input:
2
0300 0700
0400 0900
Sample Output:
1
Explanation
0720 to 0910
0830 to 0900
0845 to 1050
Since there are just 2 calls and they do not overlap, the maximum number of busy executives at any time would be 1.
Solution
// java
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] start = new int[n];
int[] end = new int[n];
for (int i = 0; i < n; i++) {
start[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
end[i] = sc.nextInt();
}
Arrays.sort(start);
Arrays.sort(end);
int max = 0;
int count = 0;
int i = 0;
int j = 0;
while (i < n && j < n) {
if (start[i] < end[j]) {
count++;
i++;
} else {
count--;
j++;
// sauravhathi
}
max = Math.max(max, count);
}
System.out.println(max);
}
}
# Python 3
n = int(input())
start = list(map(int, input().split()))
end = list(map(int, input().split()))
start.sort()
end.sort()
max = 0
count = 0
i = 0
j = 0
while i < n and j < n:
if start[i] < end[j]:
count += 1
i += 1
else:
count -= 1
j += 1
# sauravhathi
max = max(max, count)
print(max)
// C
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
int main() {
int n;
scanf("%d", &n);
int start[n];
int end[n];
for (int i = 0; i < n; i++) {
scanf("%d", &start[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &end[i]);
}
qsort(start, n, sizeof(int), compare);
qsort(end, n, sizeof(int), compare);
int max = 0;
int count = 0;
int i = 0;
int j = 0;
while (i < n && j < n) {
if (start[i] < end[j]) {
count++;
i++;
} else {
count--;
j++;
// sauravhathi
}
if (max < count) {
max = count;
}
}
printf("%d", max);
return 0;
}
// C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int start[n];
int end[n];
for (int i = 0; i < n; i++) {
cin >> start[i];
}
for (int i = 0; i < n; i++) {
cin >> end[i];
}
sort(start, start + n);
sort(end, end + n);
int max = 0;
int count = 0;
int i = 0;
int j = 0;
while (i < n && j < n) {
if (start[i] < end[j]) {
count++;
i++;
} else {
count--;
j++;
// sauravhathi
}
if (max < count) {
max = count;
}
}
cout << max;
return 0;
}
// javascript
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let n;
let start = [];
let end = [];
rl.on('line', (line) => {
if (!n) {
n = parseInt(line);
} else if (start.length < n) {
start = line.split(' ').map((x) => parseInt(x));
} else {
end = line.split(' ').map((x) => parseInt(x));
start.sort((a, b) => a - b);
end.sort((a, b) => a - b);
let max = 0;
let count = 0;
let i = 0;
let j = 0;
// sauravhathi
while (i < n && j < n) {
if (start[i] < end[j]) {
count++;
i++;
} else {
count--;
j++;
}
if (max < count) {
max = count;
}
}
console.log(max);
rl.close();
}
});
Happy Learning – If you require any further information, feel free to contact me.