TCS NQT CRUISE SHIP CODING PROBLEM WITH SOLUTION

We at gradjobopenings.com provide free job alerts of freshers job drives. In this website we list on campus job openings for freshers and off campus job openings for freshers and also work from home job openings. This is the best website to apply for off campus drive in India. Visit our website for government job alerts and private job alerts. We also list free interview notes and study materials, one of the best interview study website.comfortable to face the **interviews**:

**A party has been organised on cruise. The party is organised 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.**

**Input format**

**– First line- T (Positive integer number)**

**– Second line- T number of values, where each value is separated by a new line.**

**– Third line- T number of values, where each value is separated by a new line.**

**Output format**

**– First line: A positive integer N denoting the maximum number of people on board anytime.**

**Examples**

**– Example 1:**

** – Input :**

**5**

**7 0 5 1 3**

**1 2 1 3 4**

** – Output :**

**8**

** – Explanation:**

** – 1st hour: Entry -> 7 ; Exit -> 1 ; No. of guests on ship : 6**

** – 2nd hour: Entry -> 0 ; Exit -> 2 ; No. of guests on ship : 6-2=4**

** – 3rd hour: Entry -> 5 ; Exit -> 1 ; No. of guests on ship : 4+5-1=8**

** – 4th hour: Entry -> 1 ; Exit -> 3 ; No. of guests on ship : 8+1-3=6**

** – 5th hour: Entry -> 3 ; Exit -> 4 ; No. of guests on ship: 6+3-4=5**

** – Hence, the maximum number of guests within 5 hours is 8.**

**– Example 2:**

** – Input:**

**4**

**3 5 2 0**

**0 2 4 4**

** – Output:**

**6**

** – Explanation:**

** – Hour 1: Entry -> 3 ; Exit -> 0 ; No. of guests on ship: 3**

** – Hour 2: Entry -> 5 ; Exit -> 2 ; No. of guest on ship: 3+5-2=6**

** – Hour 3: Entry -> 2 ; Exit -> 4 ; No. of guests on ship: 6+2-4= 4**

** – Hour 4: Entry -> 0 ; Exit -> 4 ; No. of guests on ship : 4+0-4=0**

** – Hence, the maximum number of guests within 5 hours is 6.**

**Constraints:**

**– 1 <= T <= 25**

**– 0 <= E[i] <= 500**

**– 0 <= L[i] <= 500**

**Program In C**

```
#include <stdio.h>
#include <stdlib.h>
int main() {
unsigned short int T, i;
scanf("%hu", &T);
unsigned int * E = (unsigned int *) malloc (sizeof(unsigned int) * T);
unsigned int * L = (unsigned int *) malloc (sizeof(unsigned int) * T);
for (i = 0 ; i < T ; i++)
scanf("%u", &E[i]);
for (i = 0 ; i < T ; i++)
scanf("%u", &L[i]);
unsigned int current_presence = 0, max_presence = 0;
for (i = 0 ; i < T ; i++) {
current_presence += E[i] - L[i];
if (max_presence < current_presence)
max_presence = current_presence;
}
printf("%d", max_presence);
free(E);
free(L);
return 0;
}
```

Program In C++

```
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
int main() {
unsigned short int T, i;
cin >> T;
std::vector <unsigned int> E (T);
std::vector <unsigned int> L (T);
for (i = 0 ; i < T ; i++)
cin >> E.at(i);
for (i = 0 ; i < T ; i++)
cin >> L.at(i);
unsigned int current_presence {0}, max_presence {0};
for (i = 0 ; i < T ; i++) {
current_presence += E.at(i) - L.at(i);
if (max_presence < current_presence)
max_presence = current_presence;
}
cout << max_presence;
return 0;
}
```

Program In Java

```
import java.util.Scanner;
class Main {
public static void main (String args[]) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
int E[] = new int[T];
int L[] = new int[T];
for (int i = 0 ; i < T ; i++)
E[i] = in.nextInt();
for (int i = 0 ; i < T ; i++)
L[i] = in.nextInt();
in.close();
int current_presence = 0, max_presence = 0;
for (int i = 0 ; i < T ; i++) {
current_presence += E[i] - L[i];
if (max_presence < current_presence)
max_presence = current_presence;
}
System.out.print(max_presence);
}
}
```

Program In Python

```
if __name__ == '__main__':
T = int(input())
E = list(map(int, input().split()))
L = list(map(int, input().split()))
current_presence = 0
max_presence = 0
for i in range(T):
current_presence += E[i] - L[i]
if max_presence < current_presence:
max_presence = current_presence
print (max_presence, end = '')
```

Pingback: TCS NQT CODING QUESTIONS WITH SOLUTIONS - GRAD JOB OPENINGS