Most Cost Efficient Car

Easy
0/40
Average time to solve is 10m
profile
Contributed by
15 upvotes
Asked in company
Ola

Problem statement

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’.
Detailed explanation ( Input/output format, Notes, Images )
Input format:
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.
Constraints:
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
Sample Input 1:
2
100000 1200 30 60 150
200000 1300 10 20 120
80000 1000 20 40 150
60000 1300 20 100 50
Sample Output 1:
0
1
Explanation For Sample Input 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’.
Sample Input 2:
2
500000 1100 40 120 200
200000 1200 20 120 120
207120 1500 20 40 120
210000 1000 10 20 130
Sample Output 2:
1
-1
Explanation For Sample Input 2:
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’.
Hint

Can you try to compare the spending on both cars after the 6 month period?

Approaches (1)
Formula Based

Approach: The basic idea is that, find the total cost of a ‘Petrol’ car and ‘Diesel’ car for 6 months.

 

Algorithm is as follows: 

  1. Compute the Cost of both cars:
  2. Cost if we use ‘Petrol’ car:-
    • ‘ACTUALPRICE’ = Price of ‘Petrol’ car
    • ‘MAINTAINANCECOST’ = maintenance cost of ‘Petrol’ car per month’ * 6
    • ‘FUELCOST’ = per kilometer ‘Petrol’ charge * number of kilometers a ‘Petrol’ car run in a month * 6 ( for overall cost of 6 months’).
    • Per kilometer charge = ‘price of ‘Petrol/Diesel’ for a one-liter/ number of kilometers a car run in one liter.
    • Add all three costs ( ‘ACTUALPRICE’,’MAINTAINANCECOST’, ‘FUELCOST’) to get all spending on ‘Petrol’ car.
  3. In the same way, calculate the cost for a ‘Diesel’ car.
  4. Compare if ‘Petrol’ car has less cost then print ‘0’, if ‘Diesel’ car has less cost then print ‘1’ else print ‘-1’.
Time Complexity

O(1).

 

As we are using constant time.

Space Complexity

O(1).

 

As we are using constant space.

Code Solution
(100% EXP penalty)
Most Cost Efficient Car
Full screen
Console