# Meeting Scheduler in C++

C++Server Side ProgrammingProgramming

Suppose we have the availability time slots lists slots1 and slots2 of two people and a meeting duration d, we have to find the earliest time slot that works for both of them and is of duration d. If there is no common time slot that satisfies the requirements, then show an empty array. Here the format of a time slot is an array of two elements [start, end] representing an inclusive time range from start to end. we can assume that no two availability slots of the same person intersect with each other. That is, for any two time slots [s1, e1] and [s2, e2] of the same person, either s1 > e2 or s2 > e. So if the input is like s1 = [[10,50], [60,120], [140,210]] and s2 = [[0,15], [60,70]] and duration = 8, then the output will be [60,68].

To solve this, we will follow these steps −

• i := 0 and j := 0, make one array ans, sort s1 and s2.
• while i < size of s1 and j < size of s2
• end := min of s1[i, 1] and s2[j, 1]
• start := min of s1[i, 0] and s2[j, 0]
• if end – start >= duration, then
• insert start and (start + duration) into ans array, and return ans.
• otherwise when s1[i, 1] < s2[j, 1], then increase i by 1
• otherwise increase j by 1
• return ans

Let us see the following implementation to get better understanding −

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
cout << "[";
for(int i = 0; i<v.size(); i++){
cout << v[i] << ", ";
}
cout << "]"<<endl;
}
using namespace std;
bool cmp(vector <int> a, vector <int> b){
return a<b;
}
class Solution {
public:
vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) {
int i =0;
int j = 0;
vector <int> ans;
sort(slots1.begin(),slots1.end(),cmp);
sort(slots2.begin(),slots2.end(),cmp);
while(i<slots1.size() && j<slots2.size()){
int end = min(slots1[i],slots2[j]);
int start = max(slots1[i],slots2[j]);
if(end-start>=duration){
ans.push_back(start);
ans.push_back(start+duration);
return ans;
} else if(slots1[i]<slots2[j]) {
i++;
} else {
j++;}
}
return ans;
}
};
main(){
vector<vector<int>> v = {{10,50},{60,120},{140,210}};
vector<vector<int>> v1 = {{0,15},{60,70}};
Solution ob;
print_vector(ob.minAvailableDuration(v, v1, 8));
}

## Input

[[10,50],[60,120],[140,210]]
[[0,15],[60,70]]
8

## Output

[60, 68, ]