TCS NQT Menu9>
- Placement Papers
- What is TCS NQT?
- How To Apply
- Foundation Section
- Aptitude Questions
- English Verbal
- Reasoning Ability
- Advanced Section
- Advanced Quantitative Ability
- Advanced Reasoning Ability
- Advanced Coding
- Coding Questions
- Syllabus 2024
- Recruitment Process
- Registration Process
- Eligibility Criteria
- How to prepare for TCS NQT?
- Interview Questions
- Hiring Process
PREPINSTA PRIME
TCS Coding Questions 2022 Day 2 Slot 1
Coding Question 1 for 2022 (September slot)
In this article, we will discuss about the TCS Coding Question which is asked in the TCS placement test. This type of Coding Questions will help you to crack your upcoming TCS exam as well as during your inteview process.
TCS Coding Question Day 2 Slot 1 – Question 1
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.
Example 1:
Input :
- 5 -> Value of T
- [7,0,5,1,3] -> E[], Element of E[0] to E[N-1], where input each element is separated by new line
- [1,2,1,3,4] -> L[], Element of L[0] to L[N-1], while input each element is separate by new line.
Output :
8 -> Maximum number of guests on cruise at an instance.
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
Hour 3:
Entry: 5 Exit: 1
No. of guests on ship : 4+5-1=8
Hour 4:
Entry : 1 Exit : 3
No. of guests on ship : 8+1-3=6
Hour 5:
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 -> Value of T
[3,5,2,0] -> E[], Element of E[0] to E[N-1], where input each element is separated by new line.
[0,2,4,4] -> L[], Element of L[0] to L[N-1], while input each element in separated by new line
Output:
6
Cruise at an instance
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.
The input format for testing
The candidate has to write the code to accept 3 input.
First input- Accept value for number of T(Positive integer number)
Second input- Accept T number of values, where each value is separated by a new line.
Third input- Accept T number of values, where each value is separated by a new line.
The output format for testing
The output should be a positive integer number or a message as given in the problem statement(Check the output in Example 1 and Example 2)
Constraints:
- 1<=T<=25
- 0<= E[i] <=500
- 0<= L[i] <=500
import java.util.*;
class Solution
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int t = sc.nextInt ();
int e[] = new int[t];
int l[] = new int[t];
for (int i = 0; i < t; i++)
e[i] = sc.nextInt ();
for (int i = 0; i < t; i++)
l[i] = sc.nextInt ();
int max = 0, sum = 0;
for (int i = 0; i < t; i++)
{
sum += e[i] - l[i];
max = Math.max (sum, max);
}
System.out.println (max);
}
}
Using Python
n=int(input())
e=[]
l=[]
max=0
sum=0
for i in range(0,n):
el=int(input())
e.append(el)
for i in range(0,n):
li=int(input())
l.append(li)
for i in range(n-1):
sum+=e[i]-l[i]
if sum>max:
max=sum
a=sum
print(a)
#Using python
n=int(input())
e=[int(input()) for i in range(0,n)]
l=[int(input()) for i in range(0,n)]
m,c=0,e[0]-l[0]
for i in range (1,n):
c=c+e[i]-l[i]
if(c>m):
m=c
print(m)
#Code in CPP
int main()
{
int T=5;
int E[5]={7,0,5,1,3};
int L[5]={1,2,1,3,4};
int m=0,sum=0;
for(int i=0;i<T ;i++)
{
sum = sum + (E[i]-L[i]);
m = max(sum,m);
}
cout<<m;
return 0;
}
#include
using namespace std;
int main()
{
int t, mg = INT_MIN, gn = 0, time_interval = 0; // mg-Max Gusest AND gn-Gues_Now
int entry[100];
int exit[100];
cin >> t;
for (int i = 0; i > entry[i];
}
for (int i = 0; i > exit[i];
}
for (int i = 0; i < t; i++)
{
if ((gn + entry[i]) < exit[i])
{
cout << "Invalid Input" << endl;
time_interval = 0;
mg = 0;
break;
}
gn = gn + entry[i] – exit[i];
if (mg 0)
{
cout << "Maximum number of guests on cruise at an instance: " << mg << " Time Interval No:" << time_interval;
}
return 0;
}
//Python
x = int(input())
e = list(map(int,input().split(“,”)))
l = list(map(int,input().split(“,”)))
a = []
c = 0
for i in range(x):
c = c +e[i]-l[i]
a.append(c)
print(max(a))
E=[]
L=[]
T = int(input())
for i in range(T):
e=int(input())
E.append(e)
for i in range(T):
l=int(input())
L.append(l)
Sum=0
Max=0
for i in range(T):
Sum+=E[i]-L[i]
Max=max(Sum,Max)
print(“output”, Max)
E=[]
L=[]
T=int(input(“range T:”))
for i in range(T):
e=int(input(“E:”))
E.append(e)
for i in range(T):
l=int(input(“L:”))
L.append(l)
Sum=0
Max=0
for i in range(T):
Sum+=E[i]-L[i]
Max=max(Sum,Max)
print(“output”,Max)
//Using C
#include
int main()
{
int t,i,total=0,count=0;
printf(“Enter no. of hours:”);
scanf(“%d”,&t);
int a[t],b[t];
printf(“\nEnter the val of a array:”);
for(i=0;i<t;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the val of b array:");
for(i=0;i<t;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<t;i++)
{
total += a[i]-b[i];
if(totalcount)
{
count=total;
}
}
printf(“%d”,count);
return 0;
}
Using Python
time=int(input())
entry=[int(x) for x in input().split()]
exit=[int(x) for x in input().split()]
count=0
guests=[]
for i in range(len(entry)):
count=count+entry[i]-exit[i]
guests.append(count)
print(max(guests))
java code
———————————————————————————————————————————————————————————————————————————————————————- public static void main(String[] args) {
// TODO code application logic here
int t = 0;
Scanner sc = new Scanner(System.in);
t=sc.nextInt();
int e[]=new int[t];
int l[]=new int [t];
for(int i=0;i<t;i++)
{
e[i]=sc.nextInt();
}
for(int i=0;i<t;i++)
{
l[i]=sc.nextInt();
}
int v[]=new int[t];
v[0]=e[0]-l[0];
for(int i=1;i<t;i++)
{
v[i]=v[i-1]+e[i]-l[i];
}
int m=v[0];
for(int i=0;i<t;i++)
{
if(m<v[i])
m=v[i];
}
System.out.println(m);
}
using python
n = int(input(“Enter number of elements : “))
a = list(map(int,input(“\nEnter the numbers : “).strip().split()))[:n]
b = list(map(int,input(“\nEnter the numbers : “).strip().split()))[:n]
temp=[]
for i in range (0,n):
s=a[i]+b[i]
temp.append(s)
print(max(temp))
C CODE FOR THIS QUESTION
#include
#include
void main()
{
int max = INT_MIN;
int sum = 0;
int t =4;
int E[5] = {3,5,2,0};
int n = sizeof(E)/sizeof(E[0]);
int L[5] = {0,2,4,4};
int m = sizeof(L)/sizeof(L[0]);
int j =0,k=0;
for(int i=0;i<t;i++)
{
while(j<n && kmax)
{
max = sum;
}
}
}
printf(“%d”,max);
}
**while(j<n && k<m)
{
max = sum;
}
}
} printf(“%d”,max);
}
T=int(input(“value of number T: “))
E=0
L=0
N=0
Mox=0
for i in range(1,T+1):
E=int(input(“value of entry: “))
L=int(input(“value of exit: “))
N=N+E-L
Mox=max(N,Mox)
print(“maximum”,Mox)
print(“Hour”,i,”:”)
print(“Entry:”,E,” “,”Exit:”,L)
print(“No. of guests on ship:”,N)
print(“Hence,the maximum number of guests within {0} hours is {1}”.format(T+1,Mox))
java
import java.util.Scanner;
class GFG {
public static void main(String[] arg) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int e = 0;
int y[] = new int[a];
int z[] = new int[a];
for (int i = 0; i < a; i++) {
y[i] = sc.nextInt();
}
for (int i = 0; i < a; i++)
z[i] = sc.nextInt();
for (int j = 0; j <= 0; j++) {
e = y[j] – z[j];
}
for (int j = 1; j < a; j++) {
e = e + y[j] – z[j];
}
System.out.println(e);
}
}