TCS Coding Questions 2022 Day 4 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 interview process.
TCS Coding Question Day 4 Slot 1 – Question 1
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 :
-> 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”.
#include <stdio.h> int main() { int n; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); int d, x; scanf("%d %d", &d, &x); int countEven = 0, countOdd = 0; for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) countEven++; else countOdd++; } if (d % 2 != 0) { if (countEven == 0) printf("0\n"); else printf("%d\n", countEven * x); } else { if (countOdd == 0) printf("0\n"); else printf("%d\n", countOdd * x); } return 0; } }
#include<bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; int d, x; cin >> d >> x; int countEven = 0, countOdd = 0; for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) countEven++; else countOdd++; } if (d % 2 != 0) { if (countEven == 0) cout << "0" << endl; else cout << countEven * x << endl; } else { if (countOdd == 0) cout << "0" << endl; else cout << countOdd * x << endl; } return 0; }
import java.util.*; class Main { public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt (); int d = sc.nextInt (); int x = sc.nextInt (); int countEven = 0, countOdd = 0; for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) countEven++; else countOdd++; } if (d % 2 != 0) { if (countEven == 0) System.out.println ("0"); else System.out.println (countEven * x); } else { if (countOdd == 0) System.out.println ("0"); else System.out.println (countOdd * x); } } } }
n = int(input()) arr = list(map(int, input().split())) d, x = map(int, input().split()) countEven = 0 countOdd = 0 for i in range(n): if arr[i] % 2 == 0: countEven += 1 else: countOdd += 1 if d % 2 != 0: if countEven == 0: print("0") else: print(countEven * x) else: if countOdd == 0: print("0") else: print(countOdd * x)
#include
#include
using namespace std;
int main()
{
int n;
cin>>n;
vectorarr(n);
for(int i=0;i>arr[i];
int d;
cin>>d;
int x;
cin>>x;
int evencount=0;
int oddcount=0;
if(d%2==0)
{
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
evencount++;
}
int r=n-evencount;
int fine=x*r;
cout<<fine;
}
else
{
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
oddcount++;
}
int r=n-oddcount;
int fine=r*x;
cout<<fine;
}
return 0;
}