Skip to content
Related Articles

Related Articles

Improve Article

Find the Platform at which the given Train arrives

  • Difficulty Level : Hard
  • Last Updated : 29 May, 2021

Given a 2D array arr[][3] consisting of information of N trains where arr[i][0] is the train number, arr[i][1] is the arrival time, and arr[i][2] is the duration of stoppage time. Given another integer F representing the train number, the task is to find the platform number on which the train with train number F arrives according to the following rules:

  • Platform numbering starts from 1 and there is an infinite number of platforms.
  • The platform which is freed earlier is allocated to the next train.
  • If two or more platforms are freed at the same time then the train arrives at the platform with the lowest platform number.
  • If two or more trains arriving at the same time, then the train with a smaller train number is allocated first.


Input: arr[] = {{112567, 1, 2}, {112563, 3, 3}, {112569, 4, 7}, {112560, 9, 3}}, F = 112569
Output: 1
Below is the order of the arrival of trains:
Train          Platform   Leaving Time
112567        1               4
112563        2               7
112569       1              12
112560       2              13

Therefore, the train with train number 112569 arrives at platform number 1.

Input: arr[] = {{112567, 2, 1}, {112563, 5, 5}, {112569, 7, 3}, {112560, 3, 7}}, F = 112569
Output: 3

Approach: The given problem can be solved by using the priority queue. Follow the steps below to solve this problem:

  • Sort the given array arr[] of N trains according to the arrival time of the trains.
  • Initialize a priority queue, say PQ of pairs {PlatformNumber, time} that implements the min-heap according to the least departure time. Insert the {platform number, time} i.e., {1, 0} in the priority queue.
  • Initialize a HashMap, say M that stores the platform number on which any train arrives.
  • Traverse the given array arr[] and perform the following steps:
    • Pop the top platform of the PQ and store them in free_platform[].
    • If the arrival time of the current train is at least the departure time of the popped platform, then update the departure time of the popped platform as the (sum of the arrival and the stoppage time + 1) and insert the current status of the platform in PQ and the current platform number of the current train in the HashMap M.
    • Otherwise, add the new platform entry to the PQ and the current platform number of the current train in the HashMap M.
  • After completing the above steps, print the platform number associated with the train number F in the HashMap M.

Below is the implementation of the above approach: 


// Java program for the above approach
import java.util.*;
// Stores the information for each
// train as Objects
class Train {
    // Stores the train number
    String train_num;
    // Stores the arrival time
    int arrival_time;
    // Stores the stoppage time
    int stoppage_time;
    // Constructor
    Train(String train_num,
        int arrival_time,
        int stoppage_time)
        this.train_num = train_num;
        this.arrival_time = arrival_time;
        this.stoppage_time = stoppage_time;
public class GFG {
    // Function to find the platform
    // on which train F arrives
    static int findPlatformOf(
        ArrayList<Train> trains, int n,
        String F)
        // Sort the array arr[] according
        // to the arrival time
            (a, b) -> a.arrival_time==b.arrival_time ? Integer.parseInt(a.train_num)-Integer.parseInt(b.train_num) : a.arrival_time - b.arrival_time);
        // Stores the platforms that
        // is in currently in use
        PriorityQueue<int[]> pq
            = new PriorityQueue<>(
                (a, b)
                    -> a[1] == b[1] ? a[0] - b[0]
                                    : a[1] - b[1]);
        // Insert the platform number 1
        // with departure time as 0
        pq.add(new int[] { 1, 0 });
        // Store the platform number
        // on which train arrived
        HashMap<String, Integer> schedule
            = new HashMap<>();
        // Traverse the given array
        for (Train t : trains) {
            // Pop the top platform of
            // the priority queue
            int[] free_platform = pq.poll();
            // If arrival time of the train
            // >= freeing time of the platform
            if (t.arrival_time >= free_platform[1]) {
                // Update the train status
                    = t.arrival_time + t.stoppage_time + 1;
                // Add the current platform
                // to the pq
                // Add the platform
                // number to the HashMap
            // Otherwise, add a new platform
            // for the current train
            else {
                // Update the priority queue
                // Get the platform number
                int platform_num = pq.size() + 1;
                // Add the platform to
                // the priority queue
                pq.add(new int[] {
                        + t.stoppage_time + 1 });
                // Add the platform
                // number to the HashMap
        // Return the platform on
        // which F train arrived
        return schedule.get(F);
    // Driver Code
    public static void main(String[] args)
        ArrayList<Train> trains
            = new ArrayList<>();
        trains.add(new Train(
            "112567", 2, 1));
        trains.add(new Train(
            "112569", 5, 5));
        trains.add(new Train(
            "112563", 5, 3));
        trains.add(new Train(
            "112560", 3, 7));
        String F = "112563";
                trains, trains.size(), F));




Time Complexity: O(N * log N) 
Auxiliary Space: O(N)

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :