The Wayback Machine - https://web.archive.org/web/20250315071223/https://www.geeksforgeeks.org/minimum-number-platforms-required-railwaybus-station/
Open In App

Minimum Platforms Required for Given Arrival and Departure Times

Last Updated : 11 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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 at-most three trains at a time (time between 940 to 1200)

Input: arr[] = [1, 5], dep[] = [3, 7] 
Output: 1 
Explanation: Only one platform is needed. 

[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 overlapping timings 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 findPlatform(vector<int> &arr, vector<int>& dep) {
    int n = arr.size();
    int ans = 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
        ans = max(cnt, ans);
    }
    return ans;
}

int main() {

    vector<int> arr = {900, 940, 950, 1100, 1500, 1800};
    vector<int> dep = {910, 1200, 1120, 1130, 1900, 2000};
    cout << findPlatform(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 findPlatform(int[] arr, int[] dep) {
        int n = arr.length;
        int ans = 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
            ans = Math.max(cnt, ans);
        }
        return ans;
    }

    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(findPlatform(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 findPlatform(arr, dep):
    n = len(arr)
    ans = 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
        ans = max(cnt, ans)
    return ans

if __name__ == "__main__":
    arr = [900, 940, 950, 1100, 1500, 1800]
    dep = [910, 1200, 1120, 1130, 1900, 2000]
    print(findPlatform(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 findPlatform(int[] arr, int[] dep) {
        int n = arr.Length;
        int ans = 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
            ans = Math.Max(cnt, ans);
        }
        return ans;
    }

    static void Main(string[] args) {
        int[] arr = {900, 940, 950, 1100, 1500, 1800};
        int[] dep = {910, 1200, 1120, 1130, 1900, 2000};
        Console.WriteLine(findPlatform(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 findPlatform(arr, dep) {
    let n = arr.length;
    let ans = 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
        ans = Math.max(cnt, ans);
    }
    return ans;
}

let arr = [900, 940, 950, 1100, 1500, 1800];
let dep = [910, 1200, 1120, 1130, 1900, 2000];
console.log(findPlatform(arr, dep));

Output
3

[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:00Arrival1
9:10Departure 0
9:40Arrival1
9:50Arrival2
11:00Arrival3
11:20Departure 2
11:30Departure 1
12:00Departure 0
15:00Arrival1
18:00Arrival2
19:00Departure 1
20:00Departure 0

Minimum Platforms needed on railway station = Maximum platforms needed at any time = 3

To implement this approach:

  1. Sort both the arrays.
  2. Start by pointing both pointers to the beginning of the arrival and departure arrays.
  3. While the current arrival time is greater than the current departure time, it means a train has left, freeing up a platform, so we decrement the platform count and move the departure pointer forward.
  4. After freeing up the platforms, we need one platform to accommodate current train, so increment platform count.
  5. Compare the platform count with the maximum count so far.
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 findPlatform(vector<int> &arr, vector<int>& dep) {
    int n = arr.size();
    int ans = 0;

    // Sort the arrays
    sort(arr.begin(), arr.end());
    sort(dep.begin(), dep.end());
    
    int j = 0, 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++;
        
        ans = max(ans, cnt);
    }
    
    return ans;
}

int main() {

    vector<int> arr = {900, 940, 950, 1100, 1500, 1800};
    vector<int> dep = {910, 1200, 1120, 1130, 1900, 2000};
    cout << findPlatform(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 findPlatform(int[] arr, int[] dep) {
        int n = arr.length;
        int ans = 0;

        // Sort the arrays
        Arrays.sort(arr);
        Arrays.sort(dep);
        
        int j = 0, 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++;
            
            ans = Math.max(ans, cnt);
        }
        
        return ans;
    }

    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(findPlatform(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 findPlatform(arr, dep):
    n = len(arr)
    ans = 0

    # Sort the arrays
    arr.sort()
    dep.sort()
    
    j = 0
    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
        
        ans = max(ans, cnt)
    
    return ans

if __name__ == "__main__":
    arr = [900, 940, 950, 1100, 1500, 1800]
    dep = [910, 1200, 1120, 1130, 1900, 2000]
    print(findPlatform(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 findPlatform(int[] arr, int[] dep) {
        int n = arr.Length;
        int ans = 0;

        // Sort the arrays
        Array.Sort(arr);
        Array.Sort(dep);
        
        int j = 0, 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++;
            
            ans = Math.Max(ans, cnt);
        }
        
        return ans;
    }

    static void Main(string[] args) {
        int[] arr = {900, 940, 950, 1100, 1500, 1800};
        int[] dep = {910, 1200, 1120, 1130, 1900, 2000};
        Console.WriteLine(findPlatform(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 findPlatform(arr, dep) {
    let n = arr.length;
    let ans = 0;

    // Sort the arrays
    arr.sort((a, b) => a - b);
    dep.sort((a, b) => a - b);
    
    let j = 0, 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++;
        
        ans = Math.max(ans, cnt);
    }
    
    return ans;
}

let arr = [900, 940, 950, 1100, 1500, 1800];
let dep = [910, 1200, 1120, 1130, 1900, 2000];
console.log(findPlatform(arr, dep));

Output
3

[Expected Approach – 2] Using Sweep line algorithm – O(n + k) time and O(k) space

The sweep line algorithm is an efficient method for solving problems involving intervals or segment. The idea is to convert the arrival time and departure time of each train in the form of (x, y) coordinate, and then apply the sweep line algorithm to finding the maximum number of overlap at any time.

To implement this approach:

  • Firstly, create an array v[] of size greater than maximum departure time to track the number of platforms needed at each time point of time.
  • Iterate over the arrival and departure times: for each arrival time, do v[arrival time] += 1 and for each departure time, do v[departure time + 1] -= 1.
  • This process effectively marks the times when platforms are occupied and when they are free.
  • After updating the array, we calculate the cumulative sum of this array to find the maximum number of platforms needed at any time.
  • This maximum value 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 findPlatform(vector<int> &arr, vector<int>& dep) {
    int n = arr.size();
    int ans = 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];
        ans = max(ans, count);
    }
    
    return ans;
}

int main() {

    vector<int> arr = {900, 940, 950, 1100, 1500, 1800};
    vector<int> dep = {910, 1200, 1120, 1130, 1900, 2000};
    cout << findPlatform(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 findPlatform(int[] arr, int[] dep) {
        int n = arr.length;
        int ans = 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];
            ans = Math.max(ans, count);
        }
        
        return ans;
    }

    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(findPlatform(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 findPlatform(arr, dep):
    n = len(arr)
    ans = 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]
        ans = max(ans, count)
    
    return ans

if __name__ == "__main__":
    arr = [900, 940, 950, 1100, 1500, 1800]
    dep = [910, 1200, 1120, 1130, 1900, 2000]
    print(findPlatform(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 findPlatform(int[] arr, int[] dep) {
        int n = arr.Length;
        int ans = 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];
            ans = Math.Max(ans, count);
        }
        
        return ans;
    }

    static void Main(string[] args) {
        int[] arr = {900, 940, 950, 1100, 1500, 1800};
        int[] dep = {910, 1200, 1120, 1130, 1900, 2000};
        Console.WriteLine(findPlatform(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 findPlatform(arr, dep) {
    let n = arr.length;
    let ans = 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];
        ans = Math.max(ans, count);
    }
    
    return ans;
}

let arr = [900, 940, 950, 1100, 1500, 1800];
let dep = [910, 1200, 1120, 1130, 1900, 2000];
console.log(findPlatform(arr, dep));

Output
3

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.



Next Article

Similar Reads

three90RightbarBannerImg