In this post, we will see the solution of the Python Coding Question which is earlier asked in the TCS NQT Exam.
Problem Statement: Particulate matters are the biggest contributors to New-Delhi pollution. The main reason behind the increase in the concentration of Particulate matters include vehicle emission by applying Odd Even Number concept for all types of vehicles. The vehicles with the odd number last digit in the registration number will be allowed on roads on odd dates and those with even number 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: Value of n -> 5 A[], Elements A[0] to A[n-1], during input each element is separated by a space -> {5,2,3,7,9} Value of d, i.e. date -> 14 Value of x i.e. fine -> 200 Output: total fine collected -> 800 Explanation: Date d=12 means, only an even number of vehicles are allowed in this day. Find will be collected from 5,3,7 and 9 with an amount of 200 each. Hence, the output = 800. Example 2: Input: Value of n -> 6 A[], elements A[0] to A[N-1], during input each element is separated by space -> {2,5,1,6,8,4} Value of d i.e. date -> 3 Value of x i.e. fine -> 300 Output: total fine collected -> 1200 Explanation: Date d=3 means only odd number vehicles with are allowed in this day. Find will be collected from 2,6,8 and 4 with an amount of 300 each. Hence, the output = 1200 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(+ve integer) values (A[]), where each value is separated by a space. Third input – Accept value for d(+ve integer) Fourth input – Accept value for x(+ve integer ) The output format for testing: The output should be a +ve integer number (Check the output in Example 1, Example e) if no fine is collected then print ”0”.
Solution:
# taking inputs N = int(input()) A = list(map(int,input().strip().split())) d = int(input()) x = int(input()) # initialising the variable num_vehicle = 0 # check for even or odd date # based on this calculate the # rule violated vehicles and # then fine amount if d % 2 : for num in A: # count for even vehicles if num % 2 == 0 : num_vehicle += 1 else : for num in A: # count for odd vehicles if num % 2 : num_vehicle += 1 # calculated the fine amount fine_amt = num_vehicle * x print(fine_amt)
Output:
6 2 5 1 6 8 4 3 300 1200
Time Complexity: O(N)
Space Complexity: O(1)