Problem Statement:
You are given an array, you need to check if any two intervals overlap
Example:
Input: arr [] = [[1, 7], [2, 8], [3, 6]]
Output: Yes
Solution Explanation: Sorting approach
Sort the given intervals in increasing order of start time.
Then for each interval check if the start time is less than or equal to the end time of previous interval and return true.
Time Complexity: O(n logn)
Space Complexity: O(1)
Code Solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void solution(vector<vector<int>>& arr)
{
sort(arr.begin(), arr.end());
for (int i = 1; i < arr.size(); i++) {
if (arr[i][0] <= arr[i - 1][1]){
cout<<"True";
return;
}
}
cout<<"False";
return;
}
int main()
{
vector<vector<int>> arr = {{1, 7}, {2, 8}, {3, 6}};
solution(arr);
}
Output
True