A party has been organised on a cruise for a limited time (T). The number of guests entering (E[i]) and leaving (L[i]) the party at every hour is represented as elements of the array. The task is to find the maximum number of guests present on the cruise at any given instance within T hours.
Constraints:
1 <= T <= 25
0 <= E[i] <= 500
0 <= L[i] <= 500
Example 1
Input:
5 -> Value of T
[7, 0, 5, 1, 3] -> E[], elements separated by new line
[1, 2, 1, 3, 4] -> L[], elements separated by new line
Output: 8 -> Maximum number of guests on cruise at an instance.
Example 2
Input:
4 -> Value of T
[3, 5, 2, 0] -> E[], elements separated by new line
[0, 2, 4, 4] -> L[], elements separated by new line
Output: 6 -> Maximum number of guests on cruise at an instance.
Solution in:
#include<stdio.h>
#include<math.h>
int main(){
int t;
scanf("%d",&t);
int entries[t], exits[t];
for(int i=0;i<t;i++){
scanf("%d",&entries[i]);
}
int max = 0, totalIn = 0;
for(int i=0;i<t;i++){
scanf("%d",&exits[i]);
totalIn += entries[i] - exits[i];
max = (int)fmax(max, totalIn);
}
printf("%d", max);
return 0;
}
import java.util.Scanner;
public class PartyInCruise{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] entries = new int[n];
for(int i=0;i<n;i++){
entries[i] = sc.nextInt();
}
int[] exits = new int[n];
int max=0, totalIn=0;
for(int i=0;i<n;i++){
exits[i] = sc.nextInt();
totalIn += entries[i]-exits[i];
max = Math.max(max, totalIn);
}
System.out.print(max);
}
}
n = int(input())
entries = [int(input()) for i in range(n)]
exits = [int(input()) for i in range(n)]
maxCount = 0
totalIn = 0
for e1, e2 in zip(entries, exits):
totalIn += e1 - e2
maxCount = max(maxCount, totalIn)
print(maxCount)