# Minimum halls required for class scheduling

• Difficulty Level : Medium
• Last Updated : 25 May, 2021

Given N lecture timings, with their start time and end time (both inclusive), the task is to find the minimum number of halls required to hold all the classes such that a single hall can be used for only one lecture at a given time. Note that the maximum end time can be 105.
Examples:

Input: lectures[][] = {{0, 5}, {1, 2}, {1, 10}}
Output:
All lectures must be held in different halls because
at time instance 1 all lectures are ongoing.
Input: lectures[][] = {{0, 5}, {1, 2}, {6, 10}}
Output:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Approach:

• Assuming that time T starts with 0. The task is to find the maximum number of lectures that are ongoing at a particular instance of time. This will give the minimum number of halls required to schedule all the lectures.
• To find the number of lectures ongoing at any instance of time. Maintain a prefix_sum[] which will store the number of lectures ongoing at any instance of time t. For any lecture with timings between [s, t], do prefix_sum[s]++ and prefix_sum[t + 1]–.
• Afterward, the cumulative sum of this prefix array will give the count of lectures going on at any instance of time.
• The maximum value for any time instant t in the array is the minimum number of halls required.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `#define MAX 100001` `// Function to return the minimum``// number of halls required``int` `minHalls(``int` `lectures[], ``int` `n)``{` `    ``// Array to store the number of``    ``// lectures ongoing at time t``    ``int` `prefix_sum[MAX] = { 0 };` `    ``// For every lecture increment start``    ``// point s decrement (end point + 1)``    ``for` `(``int` `i = 0; i < n; i++) {``        ``prefix_sum[lectures[i]]++;``        ``prefix_sum[lectures[i] + 1]--;``    ``}` `    ``int` `ans = prefix_sum;` `    ``// Perform prefix sum and update``    ``// the ans to maximum``    ``for` `(``int` `i = 1; i < MAX; i++) {``        ``prefix_sum[i] += prefix_sum[i - 1];``        ``ans = max(ans, prefix_sum[i]);``    ``}` `    ``return` `ans;``}` `// Driver code``int` `main()``{``    ``int` `lectures[] = { { 0, 5 },``                          ``{ 1, 2 },``                          ``{ 1, 10 } };``    ``int` `n = ``sizeof``(lectures) / ``sizeof``(lectures);` `    ``cout << minHalls(lectures, n);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `java.util.*;` `class` `GFG``{``static` `int` `MAX = ``100001``;` `// Function to return the minimum``// number of halls required``static` `int` `minHalls(``int` `lectures[][], ``int` `n)``{` `    ``// Array to store the number of``    ``// lectures ongoing at time t``    ``int` `[]prefix_sum = ``new` `int``[MAX];` `    ``// For every lecture increment start``    ``// point s decrement (end point + 1)``    ``for` `(``int` `i = ``0``; i < n; i++)``    ``{``        ``prefix_sum[lectures[i][``0``]]++;``        ``prefix_sum[lectures[i][``1``] + ``1``]--;``    ``}` `    ``int` `ans = prefix_sum[``0``];` `    ``// Perform prefix sum and update``    ``// the ans to maximum``    ``for` `(``int` `i = ``1``; i < MAX; i++)``    ``{``        ``prefix_sum[i] += prefix_sum[i - ``1``];``        ``ans = Math.max(ans, prefix_sum[i]);``    ``}``    ``return` `ans;``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `lectures[][] = {{ ``0``, ``5` `},``                        ``{ ``1``, ``2` `},``                        ``{ ``1``, ``10` `}};``    ``int` `n = lectures.length;` `    ``System.out.println(minHalls(lectures, n));``}``}` `// This code is contributed by PrinciRaj1992`

## Python3

 `# Python3 implementation of the approach``MAX` `=` `100001` `# Function to return the minimum``# number of halls required``def` `minHalls(lectures, n) :` `    ``# Array to store the number of``    ``# lectures ongoing at time t``    ``prefix_sum ``=` `[``0``] ``*` `MAX``;``    ` `    ``# For every lecture increment start``    ``# point s decrement (end point + 1)``    ``for` `i ``in` `range``(n) :``        ``prefix_sum[lectures[i][``0``]] ``+``=` `1``;``        ``prefix_sum[lectures[i][``1``] ``+` `1``] ``-``=` `1``;``        ` `    ``ans ``=` `prefix_sum[``0``];``    ` `    ``# Perform prefix sum and update``    ``# the ans to maximum``    ``for` `i ``in` `range``(``1``, ``MAX``) :``        ``prefix_sum[i] ``+``=` `prefix_sum[i ``-` `1``];``        ``ans ``=` `max``(ans, prefix_sum[i]);``        ` `    ``return` `ans;` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``lectures ``=` `[[ ``0``, ``5` `],``                ``[ ``1``, ``2` `],``                ``[ ``1``, ``10` `]];``                ` `    ``n ``=` `len``(lectures);` `    ``print``(minHalls(lectures, n));` `# This code is contributed by AnkitRai01`

## C#

 `// C# implementation of the approach``using` `System;``    ` `class` `GFG``{``static` `int` `MAX = 100001;` `// Function to return the minimum``// number of halls required``static` `int` `minHalls(``int` `[,]lectures, ``int` `n)``{` `    ``// Array to store the number of``    ``// lectures ongoing at time t``    ``int` `[]prefix_sum = ``new` `int``[MAX];` `    ``// For every lecture increment start``    ``// point s decrement (end point + 1)``    ``for` `(``int` `i = 0; i < n; i++)``    ``{``        ``prefix_sum[lectures[i,0]]++;``        ``prefix_sum[lectures[i,1] + 1]--;``    ``}` `    ``int` `ans = prefix_sum;` `    ``// Perform prefix sum and update``    ``// the ans to maximum``    ``for` `(``int` `i = 1; i < MAX; i++)``    ``{``        ``prefix_sum[i] += prefix_sum[i - 1];``        ``ans = Math.Max(ans, prefix_sum[i]);``    ``}``    ``return` `ans;``}` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``int` `[,]lectures = {{ 0, 5 },``                       ``{ 1, 2 },``                       ``{ 1, 10 }};``    ``int` `n = lectures.GetLength(0);` `    ``Console.WriteLine(minHalls(lectures, n));``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``
Output:
`3`

My Personal Notes arrow_drop_up