TCS NQT Fine Calculation Coding Question With Solution
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, an attempt to reduce the pollution is made. In this method, 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.
Input format :
– First line: A non negative integer N denoting the size of the array.
– Second line: N space-separated non-negative integers.
– Third line: A non-zero positive integer, D, between 1 and 30 inclusive denoting the date.
– Fourth line: A non-negative integer X, denoting the fine in Rupees.
Output format :
– First line: A non-negative integer F, denoting the fine collected by the police.
Examples
– Input:
4
5 2 3 7
12
200
– Output:
“`
600
“`
– 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.
– Input:
5
2 5 1 6 8
3
300
– Output:
900
– 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
Program in C Language
#include <stdio.h>
int main() {
int n, a, count_odd = 0, count_even = 0, fine;
unsigned short int date;
scanf("%d", &n);
while (n--) {
scanf("%d", &a);
if (a & 1)
count_odd++;
else
count_even++;
}
scanf("%hu%d", &date, &fine);
printf("%d", date & 1 ? count_even * fine : count_odd * fine);
return 0;
}
Program in C++ Language
#include <iostream>
using std::cin;
int main() {
int n, a, count_odd = 0, count_even = 0, fine;
unsigned short int date;
cin >> n;
while (n--) {
cin >> a;
if (a & 1)
count_odd++;
else
count_even++;
}
cin >> date >> fine;
fine = date & 1 ? count_even * fine : count_odd * fine;
std::cout << fine;
return 0;
}
Program in Java
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int count_odd = 0, count_even = 0;
while (n > 0) {
int a = in.nextInt();
if (a % 2 == 1)
count_odd++;
else
count_even++;
n--;
}
int date = in.nextInt();
int fine = in.nextInt();
in.close();
fine = date % 2 == 1 ? count_even * fine : count_odd * fine;
System.out.print(fine);
}
}
Program in Python
if __name__ == '__main__':
count_odd, count_even = 0, 0
n = int(input())
a = list(map(int, input().split()))
date = int(input())
fine = int(input())
odd_count = len(list(filter(lambda x: (x%2 != 0) , a))) # Using lambda functions
# only_odd = len([num for num in a if num % 2 == 1]) # Using list comprehension
if date % 2 == 0:
print(odd_count * fine, end = '')
else:
print((len(a) - odd_count) * fine, end = '')
Pingback: TCS NQT CODING QUESTIONS WITH SOLUTIONS - GRAD JOB OPENINGS