Find the minimum capacity of the train required to hold the passengers - GeeksforGeeks

24 May, 2021
Given the number of passengers entering and exiting the train, the task is to find the minimum capacity of the train to keep all the passengers in throughout the journey.Examples:  
Input: enter[] = {3, 5, 2, 0}, exit[] = {0, 2, 4, 4} Output: 6 Station 1: Train capacity = 3 Station 2: Train capacity = 3 + 5 – 2 = 6 Station 3: Train capacity = 6 + 2 – 4 = 4 Station 4: Train capacity = 4 – 4 = 0 The maximum passengers that can be in the train at any instance of time is 6.Input: enter[] = {5, 2, 2, 0}, exit[] = {0, 2, 2, 5} Output: 5  
 
Approach: The current capacity of the train at a particular station can be calculated by adding the number of people entering the train and subtracting the number of people exiting the train. The minimum capacity required will be the maximum of all the values of current capacities at all the stations.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the minimum capacity requiredint minCapacity(int enter[], int exit[], int n){     // To store the minimum capacity    int minCap = 0;     // To store the current capacity    // of the train    int currCap = 0;     // For every station    for (int i = 0; i < n; i++) {         // Add the number of people entering the        // train and subtract the number of people        // exiting the train to get the        // current capacity of the train        currCap = currCap + enter[i] - exit[i];         // Update the minimum capacity        minCap = max(minCap, currCap);    }     return minCap;} // Driver codeint main(){    int enter[] = { 3, 5, 2, 0 };    int exit[] = { 0, 2, 4, 4 };    int n = sizeof(enter) / sizeof(enter[0]);     cout << minCapacity(enter, exit, n);     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{ // Function to return the minimum capacity requiredstatic int minCapacity(int enter[],                       int exit[], int n){     // To store the minimum capacity    int minCap = 0;     // To store the current capacity    // of the train    int currCap = 0;     // For every station    for (int i = 0; i < n; i++)    {         // Add the number of people entering the        // train and subtract the number of people        // exiting the train to get the        // current capacity of the train        currCap = currCap + enter[i] - exit[i];         // Update the minimum capacity        minCap = Math.max(minCap, currCap);    }    return minCap;} // Driver codepublic static void main(String[] args){    int enter[] = { 3, 5, 2, 0 };    int exit[] = { 0, 2, 4, 4 };    int n = enter.length;     System.out.println(minCapacity(enter, exit, n));}} // This code is contributed by Rajput-Ji
# Python3 implementation of the approach # Function to return the# minimum capacity requireddef minCapacity(enter, exit, n):         # To store the minimum capacity    minCap = 0;     # To store the current capacity    # of the train    currCap = 0;     # For every station    for i in range(n):                 # Add the number of people entering the        # train and subtract the number of people        # exiting the train to get the        # current capacity of the train        currCap = currCap + enter[i] - exit[i];         # Update the minimum capacity        minCap = max(minCap, currCap);    return minCap; # Driver codeif __name__ == '__main__':    enter = [3, 5, 2, 0];    exit = [0, 2, 4, 4];    n = len(enter);     print(minCapacity(enter, exit, n)); # This code is contributed by Princi Singh
// C# implementation of the approachusing System; class GFG{ // Function to return the minimum// capacity requiredstatic int minCapacity(int []enter,                       int []exit, int n){     // To store the minimum capacity    int minCap = 0;     // To store the current capacity    // of the train    int currCap = 0;     // For every station    for (int i = 0; i < n; i++)    {         // Add the number of people entering the        // train and subtract the number of people        // exiting the train to get the        // current capacity of the train        currCap = currCap + enter[i] - exit[i];         // Update the minimum capacity        minCap = Math.Max(minCap, currCap);    }    return minCap;} // Driver codepublic static void Main(String[] args){    int []enter = { 3, 5, 2, 0 };    int []exit = { 0, 2, 4, 4 };    int n = enter.Length;     Console.WriteLine(minCapacity(enter, exit, n));}} // This code is contributed by PrinciRaj1992
<script>// Javascript implementation of the approach  // Function to return the minimum capacity requiredfunction minCapacity(enter, exit, n) {     // To store the minimum capacity    let minCap = 0;     // To store the current capacity    // of the train    let currCap = 0;     // For every station    for (let i = 0; i < n; i++) {         // Add the number of people entering the        // train and subtract the number of people        // exiting the train to get the        // current capacity of the train        currCap = currCap + enter[i] - exit[i];         // Update the minimum capacity        minCap = Math.max(minCap, currCap);    }     return minCap;} // Driver code let enter = [3, 5, 2, 0];let exit = [0, 2, 4, 4];let n = enter.length; document.write(minCapacity(enter, exit, n)); // This code is contributed by _saurabh_jaiswal.</script>
6
 
Time Complexity: O(n) 
29AjayKumar
princiraj1992
princi singh
_saurabh_jaiswal
Arrays
Greedy
Arrays
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Window Sliding Technique
Program to find sum of elements in a given array
Reversal algorithm for array rotation
Trapping Rain Water
Find duplicates in O(n) time and O(1) extra space | Set 1
Dijkstra's shortest path algorithm | Greedy Algo-7
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Huffman Coding | Greedy Algo-3
Write a program to print all permutations of a given string