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);
}
}
ent=[3,5,2,0]
leav=[0,2,4,4]
lis=[]
a=ent[0]-leav[0]
lis.append(a)
for i in range(1,len(ent)):
a = a+ent[i]-leav[i]
lis.append(a)
print(max(lis))
Hey there,
Kindly join our Discord server for all your subject related queries.
// CPP SOLUTION WITH COMMENTS
#include
using namespace std;
int main() {
int t; // GIVEN TIME
cin >> t;
vector entry(t); // Vector to store values of ‘guests entering the cruise’
vector exit(t); // Vector to store values of ‘guests leaving the cruise’
// Input ‘entry’ values
for (int i = 0; i > entry[i];
// Input ‘exit’ values
for (int i = 0; i > exit[i];
int max = 0; // Variable to store the maximum difference
int sum = 0; // Variable to store the current cumulative difference
for (int i = 0; i < t; i++) {
sum += entry[i] – exit[i]; // Calculate the difference between 'entry' and 'exit' for the current index
max = max(sum, max); // Update 'max' with the maximum cumulative difference
}
cout << max << endl; // Output the maximum cumulative difference
return 0;
}
#include
using namespace std;
int main() {
int t; // GIVEN TIME
cin >> t;
vector entry(t); // Vector to store values of ‘guests entering the cruise’
vector exit(t); // Vector to store values of ‘guests leaving the cruise’
for (int i = 0; i> entry[i]; // Input ‘entry’ values
for (int i = 0; i exit[i]; // Input ‘entry’ values
int max = 0; // Variable to store the maximum difference
int sum = 0; // Variable to store the current cumulative difference between entry and exit
for (int i = 0; i < t; i++) {
sum += entry[i] – exit[i]; // Stores cumulative sum
max = max(sum, max); // Update 'max' with the maximum cumulative difference
}
cout << max << endl; // Output the maximum cumulative difference return 0;
}
t=int(input())
e=[]
l=[]
for i in range(t):
e.append(int(input()))
for i in range(t):
l.append(int(input()))
m=[]
a=0
rem=0
for i in range(t):
rem=rem+e[i]
rem=e[i]-l[i]
if rem>a:
a=rem
print(a)
Hey !!
Kindly join our Discord Community
python solution
time = 5
people_in = [7,0,5,1,3]
people_out = [1,2,1,3,4]
universal_count = 0
universal_count_list = list()
for duration in range(0,time):
universal_count = (universal_count+people_in[duration])-people_out[duration]
universal_count_list.append(universal_count)
print(max(universal_count_list))
Java Code by Sumanth HD
This is Prefect code Without Complexity It is Simple to Understand
package TCS;
import java.util.Iterator;
public class P5
{
public static void main(String[] args)
{
int E[]= {7,0,5,1,3};
int L[]= {1,2,1,3,4};
int Ans=0;
int max=0;
for (int i = 0; i max)
{
max=Ans;
}
}
System.out.println(“\nMaximum number of guests on cruise at an instance is “+max);
}
}
Java Code by Sumanth HD
This is Prefect code Without Complexity It is Simple to Understand
package TCS;
import java.util.Iterator;
public class P5
{
public static void main(String[] args)
{
int E[]= {7,0,5,1,3};
int L[]= {1,2,1,3,4};
int Ans=0;
int max=0;
// int arr[]=new int[5];
for (int i = 0; i max)
{
max=Ans;
}
}
System.out.println(“\nMaximum number of guests on cruise at an instance is “+max);
}
}
t=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
d=[]
c=0
for i in range(t):
c+=a[i]-b[i]
d.append(c)
print(max(d))
Python Solution:
T = int(input(‘Limited time: ‘))
E,L = [],[]
for i in range(T):
E.append(int(input(‘entry value:’)))
for i in range(T):
L.append(int(input(‘exit value:’)))
Guests = 0
maxm = 0
for i in range(T):
Guests+=E[i]-L[i]
if Guests>maxm:
maxm=Guests
print(maxm)
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)
def maxPeople():
T = int(input())
E = list(map(int,input().split()))
L = list(map(int,input().split()))
people =0
m=0
for i in range(T):
people = people + E[i]-L[i]
m = max(people,m)
return m
maxPeople()
def maxPeople():
T = int(input())
E = list(map(int,input().split()))
L = list(map(int,input().split()))
people =0
m=0
for i in range(T):
people = people + E[i]-L[i]
m = max(people,m)
return m