Total Distance TravelledProblem Statement:A tank is a dual tanked tanker. The given input comprises of two integers, namely mainTank with liters of fuel left in the main tank and additionalTank with the liters remaining in the additional tank. The truck has a milage of 10 km per liter. In case of an engine utilizing 5 liters of fuel in the main tank and the additional tank possessing at least 1 liter of fuel, 1 liter of fuel is transferred from the additional tank to the main tank. Return the maximum distance that can be covered. Java Brute Force ApproachOutput: Code Explanation: - The distanceTraveled method quantifies the overall length coveringthe vehicle carrying two tanks, that is the main tankand an extra one. It is time on every kilometer of the travel, increasing 10 units to the solution for everyone that it travels across. Suppose the existing kilometer is a multiple of five while there is an extra tank at hand.
- In that case, it expends one unit from the additional tank, runs up an additional 10 units (which means that additional capacity in the secondary tank), and falls loaded with the main tank. Through this process, there is a guarantee that the prestressed one effectively uses the capacity of the additional tank. The method has a total running distance as output.
Time Complexity: - The running time (order) of the distance traveled method is O(mainTank), based on it in each kilometer used; thus, a main tank signifies the capacity of the central tank.
Space Complexity: - The algorithm's time complexity is O (1) since it uses a constant number of spaces, regardless of the proportions.
Java Greedy ApproachOutput: Code Explanation: - The methodDistanceTraveledtracks the total distance traveled by a vehicle containing a large tank and an additional. It does a loop for every capacity of the main tanks, adding 10 quantities to the distance in each iteration. The main tank needs to consume 5 units and add it to the consumed distance with one unit from the additional tank in case it is available.
- The cycle continues until the principal resistance tank or additional resistance tank is completely utilized. When the remaining balance lasts, it determines the distance for any remaining units.
Time Complexity: - The time complexity is O(mainTank) because the algorithm goes through looping in the main tank's capacity, and the number of iterations depends on how many times the main Tank I can go around, which itself relies on the capacity of the given value.
Space Complexity: - Space complexity is O(1) because the amount per unit of extra memory space utilized by the algorithm remains constant within the scope of the past.
|