Find the minimum capacity of the train required to hold the passengers
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++
#include <bits/stdc++.h>
using namespace std;
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 = max(minCap, currCap);
}
return minCap;
}
int 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
import java.util.*;
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;
System.out.println(minCapacity(enter, exit, n));
}
}
|
Python3
def minCapacity(enter, exit, n):
minCap = 0 ;
currCap = 0 ;
for i in range (n):
currCap = currCap + enter[i] - exit[i];
minCap = max (minCap, currCap);
return minCap;
if __name__ = = '__main__' :
enter = [ 3 , 5 , 2 , 0 ];
exit = [ 0 , 2 , 4 , 4 ];
n = len (enter);
print (minCapacity(enter, exit, n));
|
C#
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));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1), no extra space is required, so it is a constant
Last Updated :
19 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...