Most Cost Efficient Car

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’.
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
CodingNinjas
author
2y
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:-...read more
Help your peers!
Add answer anonymously...
TCS System Engineer Specialist Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter