According to new Traffic rules ,Vehicles with Odd numbers can travel only in odd dates similarly Even numbered vehicles can travel only in Even dates .If any of the vehicle violates the rule will be fined Rs.100. If the same vehicle violates the rule twice the fine amount will be Rs.250 .The task is to calculate the total fine amount to be collected.
Example :
Input :
[12,30,16,15,11] // vehicle number
4 // date
output;
200
Explanation:
Since 4 is even number only even numbered vehicle can travel in that day .Odd numbered vehicles are fined Rs.100.(11 and 15 violates).Total fine amount will be Rs.200
Constraints:
The code should accept input during runtime ,It should satisfies multiple test cases.
The code can be of any programming languages except PYTHON . (Java and C programming Recommended ).
Answers
Answer:
import java.util.*;
public class HelloWorld{
public static void main(String... args){
Scanner s = new Scanner(System.in);
int[] intArray = Arrays.stream(s.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
int day = s.nextInt();
ArrayList<Integer> vis = new ArrayList<>();
int amt1 = 0;
int amt2 = 0;
if(day%2==0){
for(int i: intArray){
if(i%2!=0){
if(vis.contains(i)){
amt2 = amt2+1;
amt1 = amt1-1;
}
else{
vis.add(i);
amt1 = amt1+1;
}
}
}
}
if(day%2!=0){
for(int i: intArray){
if(i%2==0){
if(vis.contains(i)){
amt2 = amt2+1;
amt1 = amt1-1;
}
else{
vis.add(i);
amt1 = amt1+1;
}
}
}
}
int ans = amt1*100+amt2*250;
System.out.print(ans);
}
}
Explanation:
nothing much to explain.
I don't know your standards in programing
I wrote code somewhat huge but same can be written using hash set and it solved that will be a good programing but idk you have knowledge of hash sets or not so I've written this for a beginner.......
CODE:
import java.util.*;
public class TrafficRules {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of vehicles: ");
int n = sc.nextInt();
int[] vehicles = new int[n];
System.out.println("Enter vehicle numbers:");
for (int i = 0; i < n; i++) {
vehicles[i] = sc.nextInt();
}
System.out.print("Enter date: ");
int date = sc.nextInt();
int fine = 0;
for (int i = 0; i < n; i++) {
if ((vehicles[i] % 2 == 0 && date % 2 == 0) || (vehicles[i] % 2 == 1 && date % 2 == 1)) {
// Vehicle can travel on this date
continue;
} else {
// Vehicle violates rule
fine += 100;
if ((vehicles[i] % 2 == 0 && date % 2 == 0) || (vehicles[i] % 2 == 1 && date % 2 == 1)) {
// Vehicle violated rule twice
fine += 150; // 100 + 150 = 250
}
}
}
System.out.println("Total fine amount: Rs." + fine);
sc.close();
}
}
This code reads in the number of vehicles, the vehicle numbers, and the date from the user, and calculates the total fine amount based on the given rules. It assumes that the input is valid and follows the given constraints.
Note that the fine for a vehicle violating the rule twice is Rs.250, not Rs.200 as mentioned in the example output. Also note that this solution is not fully optimized and may not be suitable for large inputs.
#SPJ6