Maximum Meetings in One Room (original) (raw)

Last Updated : 3 May, 2026

Given **n meetings in the form of **start[] and **end[], where start[i] is the start time of ith meeting and end[i] is the end time of ith meeting. The task is to find the maximum number of meetings that can be scheduled in a single room. The meeting room can have only **one meeting at a particular time.

**Note: The start time of one chosen meeting can't be equal to the end time of any other chosen meeting.

**Examples:

**Input: start[] = [1, 3, 0, 5, 8, 5], end[] = [2, 4, 6, 7, 9, 9]
**Output: 1 2 4 5
**Explanation: We can attend the 1st meeting from (1 to 2), then the 2nd meeting from (3 to 4), then the 4th meeting from (5 to 7), and the 5th meeting from (8 to 9).

**Input: _start[] = [10, 12, 20], end[] = [20, 25, 30]
**Output: _1
**Explanation: _We can attend at most one meeting in a single meeting room.

Try It Yourselfredirect icon

Using Greedy Scheduling - O(n log n) Time and O(n) Space

The idea is to select the maximum number of non-overlapping meetings using a **greedy strategy.

Steps

Below is the implementation of the above approach.

C++ `

#include <bits/stdc++.h> using namespace std;

vector maxMeetings(vector &s, vector &f) {

int n = s.size();

// pair<finish_time, index>
vector<pair<int, int>> a(n);

for (int i = 0; i < n; i++) {
    a[i] = {f[i], i};
}

// sort by finish time
sort(a.begin(), a.end());

vector<int> ans;

// pick first meeting
int lastFinish = a[0].first;
ans.push_back(a[0].second + 1);

// process remaining meetings
for (int i = 1; i < n; i++) {
    if (s[a[i].second] > lastFinish) {
        ans.push_back(a[i].second + 1);
        lastFinish = a[i].first;
    }
}

// sort indices
sort(ans.begin(), ans.end());

return ans;

}

int main() { vector start = {1, 3, 0, 5, 8, 5}; vector finish = {2, 4, 6, 7, 9, 9};

vector<int> res = maxMeetings(start, finish);

for (int i = 0; i < res.size(); i++) {
    cout << res[i] << " ";
}

return 0;

}

Java

import java.util.*;

class GFG {

static List<Integer> maxMeetings(int[] s, int[] f) {

    int n = s.length;

    // pair<finish_time, index>
    int[][] a = new int[n][2];

    for (int i = 0; i < n; i++) {
        a[i][0] = f[i];
        a[i][1] = i;
    }

    // sort by finish time
    Arrays.sort(a, (x, y) -> x[0] - y[0]);

    List<Integer> ans = new ArrayList<>();

    // pick first meeting
    int lastFinish = a[0][0];
    ans.add(a[0][1] + 1);

    // process remaining meetings
    for (int i = 1; i < n; i++) {
        if (s[a[i][1]] > lastFinish) {
            ans.add(a[i][1] + 1);
            lastFinish = a[i][0];
        }
    }

    // sort indices
    Collections.sort(ans);

    return ans;
}

public static void main(String[] args) {
    int[] start = {1, 3, 0, 5, 8, 5};
    int[] finish = {2, 4, 6, 7, 9, 9};

    List<Integer> res = maxMeetings(start, finish);

    for (int x : res)
        System.out.print(x + " ");
}

}

Python

def maxMeetings(s, f):

n = len(s)

# pair<finish_time, index>
a = [(f[i], i) for i in range(n)]

# sort by finish time
a.sort()

ans = []

# pick first meeting
lastFinish = a[0][0]
ans.append(a[0][1] + 1)

# process remaining meetings
for i in range(1, n):
    if s[a[i][1]] > lastFinish:
        ans.append(a[i][1] + 1)
        lastFinish = a[i][0]

# sort indices
ans.sort()

return ans

Driver code

start = [1, 3, 0, 5, 8, 5] finish = [2, 4, 6, 7, 9, 9]

res = maxMeetings(start, finish)

for x in res: print(x, end=" ")

C#

using System; using System.Collections.Generic;

class GFG {

static List<int> MaxMeetings(int[] s, int[] f) {

    int n = s.Length;

    // pair<finish_time, index>
    List<(int, int)> a = new List<(int, int)>();

    for (int i = 0; i < n; i++) {
        a.Add((f[i], i));
    }

    // sort by finish time
    a.Sort();

    List<int> ans = new List<int>();

    // pick first meeting
    int lastFinish = a[0].Item1;
    ans.Add(a[0].Item2 + 1);

    // process remaining meetings
    for (int i = 1; i < n; i++) {
        if (s[a[i].Item2] > lastFinish) {
            ans.Add(a[i].Item2 + 1);
            lastFinish = a[i].Item1;
        }
    }

    // sort indices
    ans.Sort();

    return ans;
}

public static void Main() {
    int[] start = {1, 3, 0, 5, 8, 5};
    int[] finish = {2, 4, 6, 7, 9, 9};

    var res = MaxMeetings(start, finish);

    foreach (var x in res)
        Console.Write(x + " ");
}

}

JavaScript

function maxMeetings(s, f) {

let n = s.length;

// pair<finish_time, index>
let a = [];

for (let i = 0; i < n; i++) {
    a.push([f[i], i]);
}

// sort by finish time
a.sort((x, y) => x[0] - y[0]);

let ans = [];

// pick first meeting
let lastFinish = a[0][0];
ans.push(a[0][1] + 1);

// process remaining meetings
for (let i = 1; i < n; i++) {
    if (s[a[i][1]] > lastFinish) {
        ans.push(a[i][1] + 1);
        lastFinish = a[i][0];
    }
}

// sort indices
ans.sort((a, b) => a - b);

return ans;

}

// Driver code let start = [1, 3, 0, 5, 8, 5]; let finish = [2, 4, 6, 7, 9, 9];

let res = maxMeetings(start, finish);

console.log(res.join(" "));

`