Find the minimum capacity of the train required to hold the passengers | C # implementation of the approach ; Function to return the minimum capacity required ; To store the minimum capacity ; To store the current capacity of the train ; For every station ; 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 ; Update the minimum capacity ; Driver code
using System ; class GFG { static int minCapacity ( int [ ] enter , int [ ] exit , int n ) { int minCap = 0 ; int currCap = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currCap = currCap + enter [ i ] - exit [ i ] ; minCap = Math . Max ( minCap , currCap ) ; } return minCap ; } public 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 ) ) ; } }