Minimum Platforms Required for Given Arrival and Departure Times
Last Updated :
23 Jul, 2025
Given two arrays, arr[] and dep[], that represent the arrival and departure times of trains respectively, the task is to find the minimum number of platforms required so that no train waits.
Examples:
Input: arr[] = [900, 940, 950, 1100, 1500, 1800], dep[] = [910, 1200, 1120, 1130, 1900, 2000]
Output: 3
Explanation: There are three trains during the time 9:40 to 12:00. So we need a minimum of 3 platforms.
Input: arr[] = [1, 5], dep[] = [3, 7]
Output: 1
Explanation: All train times are mutually exclusive. So we need only one platform
[Naive Approach] Using Two Nested Loops - O(n^2) time and O(1) space
The idea is to iterate through each train and for that train, check how many other trains have overlappingtimings with it - where current train's arrival time falls between the other train's arrival and departure times. We keep track of this count for each train and continuously update our answer with the maximum count found.
C++
// C++ program to find minimum Platforms Required
// for Given Arrival and Departure Times
#include <iostream>
#include <vector>
using namespace std;
// Function to find the minimum
// number of platforms required
int minPlatform(vector<int> &arr, vector<int>& dep) {
int n = arr.size();
int res = 0;
// Run a nested for-loop to find the overlap
for (int i = 0; i < n; i++) {
// Initially one platform is needed
int cnt = 1;
for (int j = 0; j < n; j++) {
if (i != j)
// Increment cnt if trains have overlapping
// time.
if (arr[i] >= arr[j] && dep[j] >= arr[i])
cnt++;
}
// Update the result
res = max(cnt, res);
}
return res;
}
int main() {
vector<int> arr = {900, 940, 950, 1100, 1500, 1800};
vector<int> dep = {910, 1200, 1120, 1130, 1900, 2000};
cout << minPlatform(arr, dep);
return 0;
}
Java
// Java program to find minimum Platforms Required
// for Given Arrival and Departure Times
class GfG {
// Function to find the minimum
// number of platforms required
static int minPlatform(int[] arr, int[] dep) {
int n = arr.length;
int res = 0;
// Run a nested for-loop to find the overlap
for (int i = 0; i < n; i++) {
// Initially one platform is needed
int cnt = 1;
for (int j = 0; j < n; j++) {
if (i != j)
// Increment cnt if trains have overlapping
// time.
if (arr[i] >= arr[j] && dep[j] >= arr[i]) {
cnt++;
}
}
// Update the result
res = Math.max(cnt, res);
}
return res;
}
public static void main(String[] args) {
int[] arr = {900, 940, 950, 1100, 1500, 1800};
int[] dep = {910, 1200, 1120, 1130, 1900, 2000};
System.out.println(minPlatform(arr, dep));
}
}
Python
# Python program to find minimum Platforms Required
# for Given Arrival and Departure Times
# Function to find the minimum
# number of platforms required
def minPlatform(arr, dep):
n = len(arr)
res = 0
# Run a nested for-loop to find the overlap
for i in range(n):
# Initially one platform is needed
cnt = 1
for j in range(n):
if i != j:
# Increment cnt if trains have overlapping
# time.
if arr[i] >= arr[j] and dep[j] >= arr[i]:
cnt += 1
# Update the result
res = max(cnt, res)
return res
if __name__ == "__main__":
arr = [900, 940, 950, 1100, 1500, 1800]
dep = [910, 1200, 1120, 1130, 1900, 2000]
print(minPlatform(arr, dep))
C#
// C# program to find minimum Platforms Required
// for Given Arrival and Departure Times
using System;
class GfG {
// Function to find the minimum
// number of platforms required
static int minPlatform(int[] arr, int[] dep) {
int n = arr.Length;
int res = 0;
// Run a nested for-loop to find the overlap
for (int i = 0; i < n; i++) {
// Initially one platform is needed
int cnt = 1;
for (int j = 0; j < n; j++) {
if (i != j)
// Increment cnt if trains have overlapping
// time.
if (arr[i] >= arr[j] && dep[j] >= arr[i]) {
cnt++;
}
}
// Update the result
res = Math.Max(cnt, res);
}
return res;
}
static void Main(string[] args) {
int[] arr = {900, 940, 950, 1100, 1500, 1800};
int[] dep = {910, 1200, 1120, 1130, 1900, 2000};
Console.WriteLine(minPlatform(arr, dep));
}
}
JavaScript
// JavaScript program to find minimum Platforms Required
// for Given Arrival and Departure Times
// Function to find the minimum
// number of platforms required
function minPlatform(arr, dep) {
let n = arr.length;
let res = 0;
// Run a nested for-loop to find the overlap
for (let i = 0; i < n; i++) {
// Initially one platform is needed
let cnt = 1;
for (let j = 0; j < n; j++) {
if (i !== j)
// Increment cnt if trains have overlapping
// time.
if (arr[i] >= arr[j] && dep[j] >= arr[i]) {
cnt++;
}
}
// Update the result
res = Math.max(cnt, res);
}
return res;
}
// Driver Code
let arr = [900, 940, 950, 1100, 1500, 1800];
let dep = [910, 1200, 1120, 1130, 1900, 2000];
console.log(minPlatform(arr, dep));
[Expected Approach 1] Using Sorting and Two Pointers - O(n log(n)) time and O(1) space
This approach uses sorting and two-pointer to reduce the complexity. First, we sort the arrival and departure times of all trains. Then, using two pointers, we traverse through both arrays.
The idea is to maintain a count of platforms needed at any point in time.
Time | Event Type | Total Platforms Needed at this Time |
---|
9:00 | Arrival | 1 |
9:10 | Departure | 0 |
9:40 | Arrival | 1 |
9:50 | Arrival | 2 |
11:00 | Arrival | 3 |
11:20 | Departure | 2 |
11:30 | Departure | 1 |
12:00 | Departure | 0 |
15:00 | Arrival | 1 |
18:00 | Arrival | 2 |
19:00 | Departure | 1 |
20:00 | Departure | 0 |
Minimum Platforms needed on railway station = Maximum platforms needed at any time = 3
Step by Step implementation:
- Sort the arrival and departure times so we can process train timings in order.
- Initialize two pointers:
- One for tracking arrivals (
i = 0
). - One for tracking departures (
j = 0
).
- Iterate through the arrival times:
- If the current train arrives before or at the departure of an earlier train, allocate a new platform (
cnt++
). - Otherwise, if the arrival time is greater than the departure time, it means a train has left, freeing up a platform (
cnt--
), and move the departure pointer forward (j++
).
- Update the maximum number of platforms required after each step.
- Continue this process until all trains are processed.
C++
// C++ program to find minimum Platforms Required
// for Given Arrival and Departure Times
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to find the minimum
// number of platforms required
int minPlatform(vector<int> &arr, vector<int>& dep) {
int n = arr.size();
int res = 0;
// Sort the arrays
sort(arr.begin(), arr.end());
sort(dep.begin(), dep.end());
// Pointer to track the departure times
int j = 0;
// Tracks the number of platforms needed at any given time
int cnt = 0;
// Check for each train
for (int i=0; i<n; i++) {
// Decrement count if other
// trains have left
while (j<n && dep[j]<arr[i]) {
cnt--;
j++;
}
// one platform for current train
cnt++;
res = max(res, cnt);
}
return res;
}
int main() {
vector<int> arr = {900, 940, 950, 1100, 1500, 1800};
vector<int> dep = {910, 1200, 1120, 1130, 1900, 2000};
cout << minPlatform(arr, dep);
return 0;
}
Java
// Java program to find minimum Platforms Required
// for Given Arrival and Departure Times
import java.util.Arrays;
class Main {
// Function to find the minimum
// number of platforms required
static int minPlatform(int[] arr, int[] dep) {
int n = arr.length;
int res = 0;
// Sort the arrays
Arrays.sort(arr);
Arrays.sort(dep);
// Pointer to track the departure times
int j = 0;
// Tracks the number of platforms needed at any given time
int cnt = 0;
// Check for each train
for (int i = 0; i < n; i++) {
// Decrement count if other
// trains have left
while (j < n && dep[j] < arr[i]) {
cnt--;
j++;
}
// one platform for current train
cnt++;
res = Math.max(res, cnt);
}
return res;
}
public static void main(String[] args) {
int[] arr = {900, 940, 950, 1100, 1500, 1800};
int[] dep = {910, 1200, 1120, 1130, 1900, 2000};
System.out.println(minPlatform(arr, dep));
}
}
Python
# Python program to find minimum Platforms Required
# for Given Arrival and Departure Times
# Function to find the minimum
# number of platforms required
def minPlatform(arr, dep):
n = len(arr)
res = 0
# Sort the arrays
arr.sort()
dep.sort()
# Pointer to track the departure times
j = 0;
# Tracks the number of platforms needed at any given time
cnt = 0;
# Check for each train
for i in range(n):
# Decrement count if other
# trains have left
while j < n and dep[j] < arr[i]:
cnt -= 1
j += 1
# one platform for current train
cnt += 1
res = max(res, cnt)
return res
if __name__ == "__main__":
arr = [900, 940, 950, 1100, 1500, 1800]
dep = [910, 1200, 1120, 1130, 1900, 2000]
print(minPlatform(arr, dep))
C#
// C# program to find minimum Platforms Required
// for Given Arrival and Departure Times
using System;
class GfG {
// Function to find the minimum
// number of platforms required
static int minPlatform(int[] arr, int[] dep) {
int n = arr.Length;
int res = 0;
// Sort the arrays
Array.Sort(arr);
Array.Sort(dep);
// Pointer to track the departure times
int j = 0;
// Tracks the number of platforms needed at any given time
int cnt = 0;
// Check for each train
for (int i = 0; i < n; i++) {
// Decrement count if other
// trains have left
while (j < n && dep[j] < arr[i]) {
cnt--;
j++;
}
// one platform for current train
cnt++;
res = Math.Max(res, cnt);
}
return res;
}
static void Main(string[] args) {
int[] arr = {900, 940, 950, 1100, 1500, 1800};
int[] dep = {910, 1200, 1120, 1130, 1900, 2000};
Console.WriteLine(minPlatform(arr, dep));
}
}
JavaScript
// JavaScript program to find minimum Platforms Required
// for Given Arrival and Departure Times
// Function to find the minimum
// number of platforms required
function minPlatform(arr, dep) {
let n = arr.length;
let res = 0;
// Sort the arrays
arr.sort((a, b) => a - b);
dep.sort((a, b) => a - b);
// Pointer to track the departure times
let j = 0;
// Tracks the number of platforms needed at any given time
let cnt = 0;
// Check for each train
for (let i = 0; i < n; i++) {
// Decrement count if other
// trains have left
while (j < n && dep[j] < arr[i]) {
cnt--;
j++;
}
// one platform for current train
cnt++;
res = Math.max(res, cnt);
}
return res;
}
// Driver Code
let arr = [900, 940, 950, 1100, 1500, 1800];
let dep = [910, 1200, 1120, 1130, 1900, 2000];
console.log(minPlatform(arr, dep));
[Expected Approach 2] Using Sweep line algorithm
The Sweep Line Algorithm is an efficient technique for solving interval-based problems. It works by treating each train's arrival and departure times as events on a timeline. By processing these events in chronological order, we can track the number of trains at the station at any moment, which directly indicates the number of platforms required at that time. The maximum number of overlapping trains during this process determines the minimum number of platforms needed.
Step by Step implementation:
- Create an array v[] of size greater than the maximum departure time. This array will help track the number of platforms needed at each time.
- Mark arrivals and departures:
- For each arrival time, increment v[arrival_time] by 1, indicating that a platform is needed.
- For each departure time, decrement v[departure_time + 1] by 1, indicating that a platform is freed as the train has left.
- Iterate through
v[]
and compute the cumulative sum. - The running sum keeps track of the number of trains present at any given time.
- The maximum value encountered represents the minimum number of platforms required.
C++
// C++ program to find minimum Platforms Required
// for Given Arrival and Departure Times
#include <iostream>
#include <vector>
using namespace std;
// Function to find the minimum
// number of platforms required
int minPlatform(vector<int> &arr, vector<int>& dep) {
int n = arr.size();
int res = 0;
// Find the max Departure time
int maxDep = dep[0];
for (int i=1; i<n; i++) {
maxDep = max(maxDep, dep[i]);
}
// Create a vector to store the count of trains at each
// time
vector<int> v(maxDep + 2, 0);
// Increment the count at the arrival time and decrement
// at the departure time
for (int i = 0; i < n; i++) {
v[arr[i]]++;
v[dep[i] + 1]--;
}
int count = 0;
// Iterate over the vector and keep track of the maximum
// sum seen so far
for (int i = 0; i <= maxDep + 1; i++) {
count += v[i];
res = max(res, count);
}
return res;
}
int main() {
vector<int> arr = {900, 940, 950, 1100, 1500, 1800};
vector<int> dep = {910, 1200, 1120, 1130, 1900, 2000};
cout << minPlatform(arr, dep);
return 0;
}
Java
// Java program to find minimum Platforms Required
// for Given Arrival and Departure Times
import java.util.Arrays;
class GfG {
// Function to find the minimum
// number of platforms required
static int minPlatform(int[] arr, int[] dep) {
int n = arr.length;
int res = 0;
// Find the max Departure time
int maxDep = dep[0];
for (int i = 1; i < n; i++) {
maxDep = Math.max(maxDep, dep[i]);
}
// Create an array to store the count of trains at each
// time
int[] v = new int[maxDep + 2];
// Increment the count at the arrival time and decrement
// at the departure time
for (int i = 0; i < n; i++) {
v[arr[i]]++;
v[dep[i] + 1]--;
}
int count = 0;
// Iterate over the array and keep track of the maximum
// sum seen so far
for (int i = 0; i <= maxDep + 1; i++) {
count += v[i];
res = Math.max(res, count);
}
return res;
}
public static void main(String[] args) {
int[] arr = {900, 940, 950, 1100, 1500, 1800};
int[] dep = {910, 1200, 1120, 1130, 1900, 2000};
System.out.println(minPlatform(arr, dep));
}
}
Python
# Python program to find minimum Platforms Required
# for Given Arrival and Departure Times
# Function to find the minimum
# number of platforms required
def minPlatform(arr, dep):
n = len(arr)
res = 0
# Find the max Departure time
maxDep = max(dep)
# Create a list to store the count of trains at each
# time
v = [0] * (maxDep + 2)
# Increment the count at the arrival time and decrement
# at the departure time
for i in range(n):
v[arr[i]] += 1
v[dep[i] + 1] -= 1
count = 0
# Iterate over the list and keep track of the maximum
# sum seen so far
for i in range(maxDep + 2):
count += v[i]
res = max(res, count)
return res
if __name__ == "__main__":
arr = [900, 940, 950, 1100, 1500, 1800]
dep = [910, 1200, 1120, 1130, 1900, 2000]
print(minPlatform(arr, dep))
C#
// C# program to find minimum Platforms Required
// for Given Arrival and Departure Times
using System;
class GfG {
// Function to find the minimum
// number of platforms required
static int minPlatform(int[] arr, int[] dep) {
int n = arr.Length;
int res = 0;
// Find the max Departure time
int maxDep = dep[0];
for (int i = 1; i < n; i++) {
maxDep = Math.Max(maxDep, dep[i]);
}
// Create an array to store the count of trains at each
// time
int[] v = new int[maxDep + 2];
// Increment the count at the arrival time and decrement
// at the departure time
for (int i = 0; i < n; i++) {
v[arr[i]]++;
v[dep[i] + 1]--;
}
int count = 0;
// Iterate over the array and keep track of the maximum
// sum seen so far
for (int i = 0; i <= maxDep + 1; i++) {
count += v[i];
res = Math.Max(res, count);
}
return res;
}
static void Main(string[] args) {
int[] arr = {900, 940, 950, 1100, 1500, 1800};
int[] dep = {910, 1200, 1120, 1130, 1900, 2000};
Console.WriteLine(minPlatform(arr, dep));
}
}
JavaScript
// JavaScript program to find minimum Platforms Required
// for Given Arrival and Departure Times
// Function to find the minimum
// number of platforms required
function minPlatform(arr, dep) {
let n = arr.length;
let res = 0;
// Find the max Departure time
let maxDep = Math.max(...dep);
// Create an array to store the count of trains at each
// time
let v = new Array(maxDep + 2).fill(0);
// Increment the count at the arrival time and decrement
// at the departure time
for (let i = 0; i < n; i++) {
v[arr[i]]++;
v[dep[i] + 1]--;
}
let count = 0;
// Iterate over the array and keep track of the maximum
// sum seen so far
for (let i = 0; i <= maxDep + 1; i++) {
count += v[i];
res = Math.max(res, count);
}
return res;
}
// Driver Code
let arr = [900, 940, 950, 1100, 1500, 1800];
let dep = [910, 1200, 1120, 1130, 1900, 2000];
console.log(minPlatform(arr, dep));
Time Complexity: O(n + k), where n is the number of trains and k is the maximum value present in the arrays.
Auxiliary space: O(k), where k is the maximum value present in both the arrays.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem