Open In App

Find maximum and minimum cost to reach the last petrol pump

Last Updated : 14 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two integers N (N ≥ 1) and K (K ≥ 1), where N denotes the number of petrol pumps from 1 to N and K denotes the maximum capacity of a car to hold petrol in liters, then the task is to output the maximum and minimum cost for reaching to the last petrol pump from the first petrol pump, and the petrol tank should be completely empty at the last petrol pump, by following the below conditions:

  • The distance between two consecutive petrol pumps is 1 kilometer exactly.
  • The car can travel one kilometer on one liter of petrol. 
  • The cost of filling 1-liter petrol at any petrol pump is equal to its number( Formally, the first petrol pump costs 1 unit per liter, the second petrol pump costs 2 units per liter, and so on.)
  • At each station, you can fill a quantity of petrol of your own choice, But less than or equal to K.   

Note: You can’t fill petrol at any station such that petrol exceeds the capacity K and also you can’t split petrol out from the tank. 

Examples:

Input: N = 4, K = 2
Output: 6 4
Explanation: The explanation of the test case is as:

  • Maximum cost: 
    • Fill one liter petrol from 1st petrol pump, costs 1 unit and travel to 2nd petrol pump from this filled petrol.
    • Fill one liter petrol from 2nd petrol pump, costs 2 unit and travel to 3rd petrol pump from this filled petrol. 
    • Fill one liter petrol from 3rd petrol pump, costs 3 unit and travel to 4th petrol pump from this filled petrol.
    • Now, the car has reached to last petrol pump and petrol tank is also empty till here. Total maximum possible cost = 1 + 2 + 3 = 6.
  • Minimum cost: 
    • Fill two liter petrol from 1st petrol pump, costs 2*1 = 2 unit and travel to 2nd petrol pump.
    • Till here one unit petrol left, because we filled two liter at previous station and travelling from first to second pump costs 1 liter petrol. Now, fill one liter more at cost 2 unit, Then tank has 2 liter petrol in it. Car can easily go from 2nd to 4th pump in two liters. Hence, cost for reaching at last station = 2 + 2 = 4. Which is minimum possible.         

Input: N = 5, K = 3
Output: 10 5
Explanation: It can be verified that the minimum and maximum cost will be as according to the input.

Approach: Implement the idea below to solve the problem

The problem is observation based and can be solved by using some mathematics. For more clarification see the Concept of approach section below.

Concept of approach:

There are some observations related to the problem, which are listed below:

  • For minimum cost:
    • If (K  â‰¥ N – 1): If the value of K is greater than or equal to N – 1. In this case, We can fill the full tank at first station, Which will be enough for reaching last petrol pump with empty tank.
      • For example: Let K = 3 and N = 4. Here (K ≥ N – 1): 3 ≥ (4 -1). We can fill full tank at first pump station, Which will cost 1*3 = 3. In 3 liter petrol we can easily reach from 1 -> 2 -> 3 -> 4. At last station (4th), the tank is completely empty. Which is a necessary condition in the problem statement. Hence for such cases The minimum value will be (N – 1).         
    • Rest of the cases: Rest of the cases, Which doesn’t follow the above condition, In such cases the minimum cost will be  K + ((( N – K )*( N – K + 1 ))/2 ) – 1. Same can be verified by taking an example, As we took previously.
  • For maximum cost: The formula for finding maximum cost for each test case will be remain same. For maximum cost we will fill minimum amount of petrol, Which is 1 liter till the N – 1th  station. For example: Let N = 5 and K = 3. 
    • Fill 1 liter petrol at first station, costs 1 unit. Then travel first to second petrol pump.
    • Fill 1 liter petrol at second station, costs 2 unit. Then travel second to third petrol pump.
    • Fill 1 liter petrol at third station, costs 3 unit. Then travel third to fourth petrol pump.
    • Fill 1 liter petrol at fourth station, costs 4 unit. Then travel fourth to fifth petrol pump. 
    • Now, The car has reached to last station and the tank is also empty. Total maximum cost = 1+ 2 + 3 + 4 = 10. Which is maximum possible. It should be noted that the total cost is equal to the sum of natural series till N – 1. Therefore, for such cases first subtract 1 from N. formally, N -= 1 and then (N * (N + 1))/2               

Steps were taken to solve the problem:

  • Create a variable min_cost and initialize it equal to 0. 
  • If (K ≥ N – 1), then initialize min_cost equal to N – 1 else K + ((( N – K )*( N – K + 1 ))/2 ) – 1
  • N = N – 1
  • Output (N * (N – 1))/ 2 and min_cost. 

Below is the code to implement the approach:

C++




// C++ code to implement the approach
#include <iostream>
using namespace std;
 
// Method for printing max and min cost
void MinMaxCost(long long N, long long K)
{
    // Variable for holding minimum cost
    long long min_cost = 0;
 
    // Checking for the condition and
    // applying mathematical approach
    if (K >= N - 1) {
        min_cost = N - 1;
    }
    else {
        min_cost = K + (((N - K) * (N - K + 1)) / 2) - 1;
    }
    N--;
 
    // Printing maximum cost
    cout << (N * (N + 1)) / 2 << " ";
 
    // Printing minimum cost
    cout << min_cost << endl;
}
 
int main()
{
 
    // Inputs
    long long N = 5;
    long long K = 3;
 
    // Function call
    MinMaxCost(N, K);
}


Java




// Java code to implement the approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Driver Function
    public static void main(String[] args)
        throws java.lang.Exception
    {
 
        // Inputs
        long N = 5;
        long K = 3;
 
        // Function call
        MinMaxCost(N, K);
    }
 
    // Method for printing max and min cost
    static void MinMaxCost(long N, long K)
    {
        // Variable for holding minimum cost
        long min_cost = 0;
 
        // Checking for the condition and
        // applying mathematical approach
        if (K >= N - 1) {
            min_cost = N - 1;
        }
        else {
            min_cost
                = K + (((N - K) * (N - K + 1)) / 2) - 1;
        }
        N--;
 
        // Printing maximum cost
        System.out.print((N * (N + 1)) / 2 + " ");
 
        // Printing minimum cost
        System.out.println(min_cost);
    }
}


Python3




# Python3 code to implement the approach
 
# Method for printing max and min cost
 
 
def min_max_cost(N: int, K: int) -> None:
    # Variable for holding minimum cost
    min_cost = 0
 
    # Checking for the condition and
    # applying mathematical approach
    if K >= N - 1:
        min_cost = N - 1
    else:
        min_cost = K + (((N - K) * (N - K + 1)) // 2) - 1
 
    N -= 1
 
    # Printing maximum cost
    print((N * (N + 1)) // 2, end=" ")
 
    # Printing minimum cost
    print(min_cost)
 
 
# Driver code
if __name__ == "__main__":
    # Inputs
    N = 5
    K = 3
 
    # Function call
    min_max_cost(N, K)


C#




using System;
 
public class Program {
    // Method for printing max and min cost
    public static void MinMaxCost(long N, long K)
    {
        // Variable for holding minimum cost
        long min_cost = 0;
 
        // Checking for the condition and
        // applying mathematical approach
        if (K >= N - 1) {
            min_cost = N - 1;
        }
        else {
            min_cost
                = K + (((N - K) * (N - K + 1)) / 2) - 1;
        }
        N--;
 
        // Printing maximum cost
        Console.Write((N * (N + 1)) / 2 + " ");
 
        // Printing minimum cost
        Console.Write(min_cost + "\n");
    }
 
    public static void Main()
    {
        // Inputs
        long N = 5;
        long K = 3;
 
        // Function call
        MinMaxCost(N, K);
    }
}


Javascript




// JavaScript code to implement the approach
 
// Method for printing max and min cost
function MinMaxCost(N, K) {
    // Variable for holding minimum cost
    let min_cost = 0;
 
    // Checking for the condition and
    // applying mathematical approach
    if (K >= N - 1) {
        min_cost = N - 1;
    }
    else {
        min_cost = K + (((N - K) * (N - K + 1)) / 2) - 1;
    }
    N--;
 
    // Printing maximum and minimum cost
    console.log((N * (N + 1)) / 2 + " " + min_cost);
 
    console.log();
}
 
// Driver code
 
// Inputs
const N = 5;
const K = 3;
 
// Function call
MinMaxCost(N, K);


Output

10 5

Time Complexity: O(1)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads