TCS NQT Coding Question Day 4 Slot 1 – Question 2
Problem Statement
Particulate matters are the biggest contributors to Delhi pollution. The main reason behind the increase in the concentration of PMs include vehicle emission by applying Odd Even concept for all types of vehicles. The vehicles with the odd last digit in the registration number will be allowed on roads on odd dates and those with even last digit will on even dates.
Given an integer array a[], contains the last digit of the registration number of N vehicles traveling on date D(a positive integer). The task is to calculate the total fine collected by the traffic police department from the vehicles violating the rules.
Note : For violating the rule, vehicles would be fined as X Rs.
Example 1:
Input :
4 -> Value of N
{5,2,3,7} -> a[], Elements a[0] to a[N-1], during input each element is separated by a new line
12 -> Value of D, i.e. date
200 -> Value of x i.e. fine
Output :
600 -> total fine collected
Explanation:
Date D=12 means , only an even number of vehicles are allowed.
Find will be collected from 5,3 and 7 with an amount of 200 each.
Hence, the output = 600.
Example 2:
Input :
5 -> Value of N
{2,5,1,6,8} -> a[], elements a[0] to a[N-1], during input each element is separated by new line
3 -> Value of D i.e. date
300 -> Value of X i.e. fine
Output :
900 -> total fine collected
Explanation:
Date D=3 means only odd number vehicles with are allowed.
Find will be collected from 2,6 and 8 with an amount of 300 each.
Hence, the output = 900
Constraints:
- 0<N<=100
- 1<=a[i]<=9
- 1<=D <=30
- 100<=x<=5000
The input format for testing
The candidate has to write the code to accept 4 input(s).
First input – Accept for N(Positive integer) values (a[]), where each value is separated by a new line.
Third input – Accept value for D(Positive integer)
Fourth input – Accept value for X(Positive integer )
The output format for testing
The output should be a positive integer number (Check the output in Example 1, Example e) if no fine is collected then print ”0”.
Day 3 Slot 2 – Question 1
python code
a_count = 0
string = input()
L = int(input())
prev_count = 0
curr_count = 0
count = 0
for char in string:
if count == L:
count = 0
prev_count = max(prev_count, curr_count)
curr_count = 0
if char == ‘a’:
curr_count += 1
count += 1
# print(count, char, curr_count, prev_count)
print(max(curr_count, prev_count))
Day 3 Slot 2 – Question 2
Python code
def factorial(N):
if N == 1:
return 1
return N*factorial(N-1)
N = int(input())
print(factorial(N-1)*2)
Day 4 Slot 1 – Question 2
Python code
`
N = int(input())
odd_count = 0
even_count = 0
for _ in range(N):
n = int(input())
if n%2 == 0:
even_count += 1
else:
odd_count += 1
D = int(input())
X = int(input())
result = X
result *= odd_count if D%2 == 0 else even_count
print(result)
`
#include
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
int d,x;
int cnt=0;
int fine=0;
cout<<"Enter date and fine amount"<>d>>x;
cout<<"Enter the array elements"<<endl;
for(int i=0;i>arr[i];
if(d%2==0)
{
if(arr[i]%2!=0)
{
cnt++;
}
}
else if(d%2!=0)
{
if(arr[i]%2==0)
{
cnt++;
}
}
}
fine=cnt*x;
cout<<"The fine amount to be payed is "<<fine<<endl;
return 0;
}
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int d,x,n;
System.out.println(“enter date “);
d=sc.nextInt();
System.out.println(“enter fine “);
x=sc.nextInt();
System.out.println(“enter size “);
n=sc.nextInt();
int arr[]= new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int f=0;
if(d%2==0)
{
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
f++;
}
int res=f*x;
System.out.println(res);
}
else if(d%2!=0)
{
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
f++;
}
int res=f*x;
System.out.println(res);
}
// int res=f*x;
// System.out.println(f);
}