Given a 2D array arr[][] where arr[i][0] represents the starting time and arr[i][1] represents the ending time of the ith meeting, determine whether it is possible for a person to attend all meetings without any overlaps, considering that a person can attend only one meeting at any given time.
Note: A person can also attend a meeting if it's starting time is same as the previous meeting's ending time.
Examples:
Input: arr[][] = [[2, 4], [1, 2], [7, 8], [5, 6], [6, 8]]
Output: false
Explanation: Since the third and fifth meeting overlap, a person cannot attend all the meetings.Input: arr[][] = [[1, 4], [10, 15], [7, 10]]
Output: true
Explanation: Since all the meetings are held at different times, it is possible to attend all the meetings.
Table of Content
[Naive Approach] By checking all pairs of meetings - O(n^2) Time and O(1) Space
A person won't be able to attend all the meetings if any two meetings overlap with each other. So, we can use nested loops to iterate over all pairs of meetings and check if any pair of meetings overlap with each other. If no meetings overlap, then it is possible to attend all the meetings, else it is impossible to attend all the meetings.
#include <iostream>
#include <vector>
using namespace std;
// function to check if two meeting overlap
bool isOverlap(vector<int> &meet1, vector<int> &meet2) {
return (meet1[0] >= meet2[0] && meet1[0] < meet2[1]) ||
(meet2[0] >= meet1[0] && meet2[0] < meet1[1]);
}
// function to check if a person can attend all the meetings
bool canAttend(vector<vector<int>> &arr) {
int n = arr.size();
for(int i = 0; i < n - 1; i++) {
for(int j = i + 1; j < n; j++) {
// If any two meetings overlap, then a person
// cannot attend all the meetings
if(isOverlap(arr[i], arr[j]))
return false;
}
}
return true;
}
int main() {
vector<vector<int>> arr = {{2, 4}, {1, 2}, {7, 8}, {5, 6}, {6, 8}};
cout << (canAttend(arr) ? "true" : "false");
return 0;
}
import java.util.ArrayList;
import java.util.Arrays;
class GfG {
// function to check if two meetings overlap
static boolean isOverlap(int[] meet1, int[] meet2) {
return (meet1[0] >= meet2[0] && meet1[0] < meet2[1]) ||
(meet2[0] >= meet1[0] && meet2[0] < meet1[1]);
}
// function to check if a person can attend all the meetings
static boolean canAttend(int[][] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// If any two meetings overlap, then a person
// cannot attend all the meetings
if (isOverlap(arr[i], arr[j])) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
int[][] arr = {{2, 4}, {1, 2}, {7, 8}, {5, 6}, {6, 8}};
System.out.println(canAttend(arr));
}
}
# function to check if two meetings overlap
def isOverlap(meet1, meet2):
return (meet1[0] >= meet2[0] and meet1[0] < meet2[1]) or \
(meet2[0] >= meet1[0] and meet2[0] < meet1[1])
# function to check if a person can attend all the meetings
def canAttend(arr):
n = len(arr)
for i in range(n - 1):
for j in range(i + 1, n):
# If any two meetings overlap, then a person
# cannot attend all the meetings
if isOverlap(arr[i], arr[j]):
return False
return True
if __name__ == "__main__":
arr = [[2, 4], [1, 2], [7, 8], [5, 6], [6, 8]]
print("true" if canAttend(arr) else "false")
using System;
class GfG {
// Function to check if two meetings overlap
static bool IsOverlap(int[] meet1, int[] meet2) {
return (meet1[0] >= meet2[0] && meet1[0] < meet2[1]) ||
(meet2[0] >= meet1[0] && meet2[0] < meet1[1]);
}
// Function to check if a person can attend all the meetings
static bool CanAttend(int[,] arr) {
int n = arr.GetLength(0);
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int[] meet1 = { arr[i, 0], arr[i, 1] };
int[] meet2 = { arr[j, 0], arr[j, 1] };
// If any two meetings overlap, return false
if (IsOverlap(meet1, meet2))
return false;
}
}
return true;
}
static void Main() {
int[,] arr = {
{2, 4},
{1, 2},
{7, 8},
{5, 6},
{6, 8}
};
Console.WriteLine(CanAttend(arr) ? "true" : "false");
}
}
// function to check if two meetings overlap
function isOverlap(meet1, meet2) {
return (meet1[0] >= meet2[0] && meet1[0] < meet2[1]) ||
(meet2[0] >= meet1[0] && meet2[0] < meet1[1]);
}
// function to check if a person can attend all the meetings
function canAttend(arr) {
let n = arr.length;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
// If any two meetings overlap, then a person
// cannot attend all the meetings
if (isOverlap(arr[i], arr[j])) {
return false;
}
}
}
return true;
}
// Driver Code
const arr = [[2, 4], [1, 2], [7, 8], [5, 6], [6, 8]];
console.log(canAttend(arr) ? "true" : "false");
Output
false
[Expected Approach] Using Sorting - O(n*log(n)) Time and O(1) Space
Check whether all meetings can be attended without time conflicts by first sorting the meeting intervals based on their start times. After sorting, it compares each meeting's end time with the next meeting's start time to detect any overlaps. If an overlap is found, it returns
false, indicating a scheduling conflict. Otherwise, it returnstrue, meaning all meetings can be attended without overlaps.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool canAttend(vector<vector<int>> &arr) {
int n = arr.size();
// Sort the meetings by their start times
sort(arr.begin(), arr.end());
for(int i = 0; i < n - 1; i++) {
// Compare the current meeting's end time with the
// next meeting's start time to check for overlap
if(arr[i][1] > arr[i + 1][0])
return false;
}
return true;
}
int main() {
vector<vector<int>> arr = {{2, 4}, {1, 2}, {7, 8}, {5, 6}, {6, 8}};
cout << (canAttend(arr) ? "true" : "false");
return 0;
}
import java.util.Arrays;
class GfG {
static boolean canAttend(int[][] arr) {
int n = arr.length;
// Sort the meetings by their start times
Arrays.sort(arr, (a, b) -> Integer.compare(a[0], b[0]));
for (int i = 0; i < n - 1; i++) {
// Compare the current meeting's end time with the
// next meeting's start time to check for overlap
if (arr[i][1] > arr[i + 1][0])
return false;
}
return true;
}
public static void main(String[] args) {
int[][] arr = {{2, 4}, {1, 2}, {7, 8}, {5, 6}, {6, 8}};
System.out.println(canAttend(arr));
}
}
def canAttend(arr):
n = len(arr)
# Sort the meetings by their start times
arr.sort(key=lambda x: x[0])
for i in range(n - 1):
# Compare the current meeting's end time with the
# next meeting's start time to check for overlap
if arr[i][1] > arr[i + 1][0]:
return False
return True
if __name__ == "__main__":
arr = [[2, 4], [1, 2], [7, 8], [5, 6], [6, 8]]
print("true" if canAttend(arr) else "false")
using System;
class GfG {
// Function to check if a person can attend all the meetings
static bool canAttend(int[][] arr) {
int n = arr.Length;
// Sort the meetings by their start times
Array.Sort(arr, (a, b) => a[0].CompareTo(b[0]));
for (int i = 0; i < n - 1; i++) {
// Compare the current meeting's end time with the
// next meeting's start time to check for overlap
if (arr[i][1] > arr[i + 1][0])
return false;
}
return true;
}
static void Main() {
int[][] arr = new int[][] {
new int[] {2, 4},
new int[] {1, 2},
new int[] {7, 8},
new int[] {5, 6},
new int[] {6, 8}
};
Console.WriteLine(canAttend(arr) ? "true" : "false");
}
}
// Function to check if a person can attend all the meetings
function canAttend(arr) {
const n = arr.length;
// Sort the meetings by their start times
arr.sort((a, b) => a[0] - b[0]);
for (let i = 0; i < n - 1; i++) {
// Compare the current meeting's end time with the
// next meeting's start time to check for overlap
if (arr[i][1] > arr[i + 1][0]) {
return false;
}
}
return true;
}
// Driver code
const arr = [[2, 4], [1, 2], [7, 8], [5, 6], [6, 8]];
console.log(canAttend(arr));
Output
false