You are given the price of ‘Petrol’ cars, the maintenance cost of the car per month, number of kilometers a car runs in one liter, amount of Petrol per liter, and number of kilometers a car can run per month. Similar details are also given for ‘Diesel’ cars.
Your task is to find which car ( ‘Petrol’ or ‘Diesel’ ) is efficient to use for an exact 6 month period. If your answer is ‘Petrol’ car then print ‘0’, if your answer is ‘Diesel’ car then print ‘1’, if both the cars ( ‘Petrol’ and ‘Diesel’) are efficient then print ‘-1’.
For example, consider both the cars:
Price of ‘Petrol’ car = ‘780000’, the maintenance cost of ‘Petrol’ car per month = ‘500’, number of kilometers a ‘Petrol’ car can run in one liter = ’35’, price of ‘Petrol’ per liter = ‘75’ and number of kilometers a ‘Petrol’ car can run in a month = ‘145’.
Similar details are given for ‘Diesel’ car, price of ‘Diesel’ car = ‘850000’, the maintenance cost of ‘Diesel’ car per month = ‘1000’, number of kilometers a ‘Diesel’ car can run in a one-liter = ’30’, price of ‘Diesel’ per liter = ‘80’ and number of kilometers a ‘Diesel’ car can run in a month = ‘200’.
Then the ‘Petrol’ car will be more efficient, so we will print the integer ‘0’.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘2*T’ lines represent the ‘T’ test cases.
The first line of each test case contains the 5 space-separated integers which denote the price of a ‘Petrol’ car, maintenance cost of ‘Petrol’ car per month, number of kilometers a ‘Petrol’ car runs in one liter, price of ‘Petrol’ per liter, number of kilometers run a ‘Petrol’ car per month.
The second line of each test case contains the 5 space-separated integers which denote the price of a ‘Diesel’ car, the maintenance cost of ‘Diesel’ car per month, the number of kilometers a ‘Diesel’ car runs in one liter, the price of ‘Diesel’ per liter, number of kilometers run a ‘Diesel’ car per month.
In the main function ‘mostCostEfficientCar’, ‘petrolCar’, ‘dieselCar’ are two objects of ‘Car’ class, and allow to access all parameters (priceOfCar, maintenanceCostPerMonth, numberOfkilemeterCarRunInOneLiter, pricePerLiter, numberOfKilometerCarRunInaMonth) by pointer.
Output Format:
For each test case, print ‘0’, ‘1’, and ‘-1’ accordingly, if your answer is ‘Petrol’ car then print ‘0’, if your answer is ‘Diesel’ car then print ‘1’, if both the cars ( ‘Petrol’ and ‘Diesel’) are efficient then print ‘-1’.
Note:
You are not required to print the output explicitly, it has already been taken care of. Just implement the function.
1 <= T <= 10^5
1 <= priceOfCar, maintenanceCostPerMonth, numberOfkilemeterCarRunInOneLiter, pricePerLiter, numberOfKilometerCarRunInaMonth <= 10^9
Where ‘T’ is the total number of test cases.
Time Limit: 1 sec
2
100000 1200 30 60 150
200000 1300 10 20 120
80000 1000 20 40 150
60000 1300 20 100 50
0
1
In the first test case:
Cost if we use ‘Petrol’ car = 100000 ( price of ‘Petrol’ car ) + 1200 * 6 ( maintenance cost of ‘Petrol’ car for 6 months ) + 60/30 * 150 * 6 ( cost of ‘Petrol’ for 6 month).
Total cost = ‘100000+ 7200 + 1800 = 109000’.
Cost if we use ‘Diesel’ car = 200000 ( price of ‘Diesel’ car ) + 1300 * 6 ( maintenance cost of ‘Diesel’ car for 6 months ) + 20/10 * 120 * 6 ( cost of ‘Diesel’ for 6 month).
Total cost = ‘200000+ 7800 + 1440 = 209240’.
The cost of a ‘Diesel’ car (209240) is more than a ‘Petrol’ (109000) car than a ‘Petrol’ car is more cost-efficient, print ‘0’.
In the second test case:
Cost if we use ‘Petrol’ car = 80000 ( price of ‘Petrol’ car ) + 1000 * 6 ( maintenance cost of ‘Petrol’ car for 6 months ) + 40/20 * 150 * 6 ( cost of ‘Petrol’ for 6 month).
Total cost = ‘80000+ 6000 + 1800 = 87800’
Cost if we use ‘Diesel’ car = 60000 ( price of ‘Diesel’ car ) + 1300 * 6 ( maintenance cost of ‘Diesel’ car for 6 months ) + 100/20 * 50 * 6 ( cost of ‘Diesel’ for 6 month).
Total cost = ‘60000+ 7800 + 1500 = 69300’
The cost of ‘Petrol’ car (87800) is more than ‘Diesel’ (67500) car than ‘Diesel’ car is more cost-efficient, print ‘1’.
2
500000 1100 40 120 200
200000 1200 20 120 120
207120 1500 20 40 120
210000 1000 10 20 130
1
-1
In the first test case:
Cost if we use ‘Petrol’ car = 500000 ( price of ‘Petrol’ car ) + 1100 * 6 ( maintenance cost of ‘Petrol’ car for 6 months ) + 120/40 * 200 * 6 ( cost of ‘Petrol’ for 6 month).
Total cost = ‘500000+ 6600 + 3600 = 510200’.
Cost if we use ‘Diesel’ car = 200000 ( price of ‘Diesel’ car ) + 1200 * 6 ( maintenance cost of ‘Diesel’ car for 6 months ) + 120/20 * 120 * 6 ( cost of ‘Diesel’ for 6 month).
Total cost = ‘200000+ 7200 + 4320 = 211520’.
The cost of a ‘Diesel’ car (211520) is less than a ‘Petrol’ (510200) car so a ‘Diesel’ car is more cost-efficient, print ‘1’.
In the second test case:
Cost if we use ‘Petrol’ car = 207120 ( price of ‘Petrol’ car ) + 1500 * 6 ( maintenance cost of ‘Petrol’ car for 6 months ) + 40/20 * 120 * 6 ( cost of ‘Petrol’ for 6 month).
Total cost = ‘207120 + 9000 + 1440 = 217560’
Cost if we use ‘Diesel’ car = 210000 ( price of ‘Diesel’ car ) + 1000 * 6 ( maintenance cost of ‘Diesel’ car for 6 months ) + 20/10 * 130 * 6 ( cost of ‘Diesel’ for 6 month).
Total cost = ‘210000 + 6000 + 1560 = 217560’
The cost of ‘Petrol’ car (217560) is the same as ‘Diesel’ (217560) car thus both cars are more cost-efficient, print ‘-1’.
Can you try to compare the spending on both cars after the 6 month period?
Approach: The basic idea is that, find the total cost of a ‘Petrol’ car and ‘Diesel’ car for 6 months.
Algorithm is as follows:
O(1).
As we are using constant time.
O(1).
As we are using constant space.