Minimum Platforms Required (Train Problem) in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
#include <stdlib.h>
int cmp(const void* a,const void* b){return (*(int*)a-*(int*)b);}
int main(){
    int arr[]={900,940,950,1100,1500,1800}, dep[]={910,1200,1120,1130,1900,2000},n=6;
    qsort(arr,n,sizeof(int),cmp); qsort(dep,n,sizeof(int),cmp);
    int plat=1,res=1,i=1,j=0;
    while(i<n && j<n){
        if(arr[i]<=dep[j]){plat++; i++; if(plat>res) res=plat;}
        else{plat--; j++;}
    }
    printf("%d",res);
}

C Output

Input:  
arr = {900, 940, 950, 1100, 1500, 1800}, dep = {910, 1200, 1120, 1130, 1900, 2000}  

Output:  
3


C++ Program

#include <bits/stdc++.h>
using namespace std;
int main(){
    vector<int> arr={900,940,950,1100,1500}, dep={920,1200,1120,1130,1900};
    sort(arr.begin(),arr.end()); sort(dep.begin(),dep.end());
    int plat=1,res=1,i=1,j=0,n=arr.size();
    while(i<n && j<n){
        if(arr[i]<=dep[j]){plat++; res=max(res,plat); i++;}
        else{plat--; j++;}
    }
    cout<<res;
}

C++ Output

Input:  
arr = {900, 940, 950, 1100, 1500}, dep = {920, 1200, 1120, 1130, 1900} 

Output:  
2


JAVA Program

import java.util.*;
class Main{
    public static void main(String[] args){
        int[] arr={900,905,915,940}, dep={920,930,945,1000};
        Arrays.sort(arr); Arrays.sort(dep);
        int plat=1,res=1,i=1,j=0,n=arr.length;
        while(i<n && j<n){
            if(arr[i]<=dep[j]){plat++; res=Math.max(res,plat); i++;}
            else{plat--; j++;}
        }
        System.out.println(res);
    }
}

JAVA Output

Input:  
arr = {900, 905, 915, 940}, dep = {920, 930, 945, 1000}  

Output:  
3


Python Program

arr=[1000,1010,1025]
dep=[1030,1040,1050]
arr.sort(); dep.sort()
plat=res=1; i=1; j=0; n=len(arr)
while i<n and j<n:
    if arr[i]<=dep[j]:
        plat+=1; res=max(res,plat); i+=1
    else:
        plat-=1; j+=1
print(res)

Python Output

Input:  
arr = [1000, 1010, 1025], dep = [1030, 1040, 1050] 

Output:  
1


Deep Explanation
Example
Imagine trains arriving and leaving from a station. If one train arrives before another departs, they both need platforms. If many overlap in time, more platforms are needed. The goal is to find the minimum number that can handle the schedule without delays.

Real-Life Analogy
Consider parking spaces rather than platforms. If they come and go at various times, you require enough spaces to accommodate all vehicles parked at the same moment. It is much like trains and platforms, just horizontal rather than vertical.

Learning Insights
This exercise is an excellent introduction to interval overlap resolution. Rather than comparing each train with every other (O(n²)), sorting arrival and departure times and scanning them sequentially provides an O(n log n) solution. It illustrates how preprocessing data (sorting) can cut complexity by a tremendous amount.

Why It Matters
In interviews, this is usually used to assess your ability to optimize time complexity and manage multiple sequences simultaneously. Two-pointer scan following sorting is a standard technique that will recur in many unrelated questions, such as merging meeting calendars, synchronizing video playback, or handling server logs.

Real-World Use
Railway timetabling, airport gate assignment, appointment scheduling, and even cloud computing resource scheduling can be applied using this algorithm. Wherever you have start and end times, you can apply this technique to determine resource demands. 

SEO-Friendly Wrap-Up
The minimum platforms problem shows you how to solve overlapping intervals with the most efficient method of sorting and scanning using two pointers. This is not only useful for solving train scheduling problems, but also for airport runway usage, conference room reservations, and network bandwidth assignment. Practicing this logic enhances your skill at managing real-time resources and acing popular algorithm interview questions.