Find the minimum capacity of the train required to hold the passengers | 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
function minCapacity ( enter , exit , n ) { let minCap = 0 ; let currCap = 0 ; for ( let i = 0 ; i < n ; i ++ ) { currCap = currCap + enter [ i ] - exit [ i ] ; minCap = Math . max ( minCap , currCap ) ; } return minCap ; } let enter = [ 3 , 5 , 2 , 0 ] ; let exit = [ 0 , 2 , 4 , 4 ] ; let n = enter . length ; document . write ( minCapacity ( enter , exit , n ) ) ;