Given an integer N which is the number of villagers who need to cross a river but there is only one boat on which a maximum of 2 person can travel. Each person i has to pay some specific price Pi to travel alone in the boat. If two person i, j travel in the boat then they have to pay max(Pi, Pj). The task is to find the minimum amount all the villagers have to pay to cross the river.
Examples:Â
Â
Input: Price[] = {30, 40, 60, 70}Â
Output: 220Â
P1 and P2 go together (which costs 40)Â
and P1 comes back (total cost 70 now).Â
Now P3 and P4 go (total cost 140) andÂ
P2 comes back (total cost 180) andÂ
finally P1 and P2 go together (total cost 220).
Input: Price[] = {892, 124}Â
Output: 892Â
Â
Â
Approach: There are two ways for the two most costly person to cross the river:Â
Â
- Both of them cross the river with the cheapest person turn by turn. So, the total cost will be the cost of the two costly persons + 2 * (cost of the cheapest person) (due to coming back).
- The two cheapest person cross the river and the cheapest person comes back. Now, the 2 most costly person cross the river and 2nd cheapest person comes back. So, the total cost will be the cost of the cheapest and costliest person plus 2 * cost of the second cheapest person.
- Total cost will be the minimum of the above two ways.
Â
Let’s consider the example we used above to understand the approach:Â
P1 = 30, P2 = 40, P3 = 60, P4 = 70
According to first method, P4 goes with P1 and P1 comes back (cost is P4+P1).Â
Now, P3 goes with P1 and P1 comes back (cost for this ride will be P4+P1).Â
So, total cost for sending two most costly person according to method 1 is P4+2*P1+P3 = 190
According to second method, P2 goes with P1 and P1 comes back (cost is P2+P1).Â
Now, P3 goes with P4 and P2 comes back (cost for this ride will be P4+P2).Â
So, total cost for sending two most costly person according to method 2 is P4+2*P2+P1 = 180
Hence, cost for sending P3 and P4 will be minimum of 2 methods, i.e., 180.
Now, we are left with P1 and P2 whom we have to send together and cost willÂ
be P2 = 40.
So, total cost for travelling is 180 + 40 = 220.Â
Â
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
#define ll long long intÂ
// Function to return the minimum costint minimumCost(ll price[], int n){Â
    // Sort the price array    sort(price, price + n);    ll totalCost = 0;Â
    // Calculate minimum price    // of n-2 most costly person    for (int i = n - 1; i > 1; i -= 2) {        if (i == 2) {            totalCost += price[2] + price[0];        }        else {Â
            // Both the ways as discussed above            ll price_first = price[i] + price[0] + 2 * price[1];            ll price_second = price[i] + price[i - 1] + 2 * price[0];            totalCost += min(price_first, price_second);        }    }Â
    // Calculate the minimum price    // of the two cheapest person    if (n == 1) {        totalCost += price[0];    }    else {        totalCost += price[1];    }Â
    return totalCost;}Â
// Driver codeint main(){Â Â Â Â ll price[] = { 30, 40, 60, 70 };Â Â Â Â int n = sizeof(price) / sizeof(price[0]);Â
    cout << minimumCost(price, n);Â
    return 0;} |
Java
// Java implementation of the approachimport java.util.*;Â
class GFG{         // Function to return the minimum cost    static long minimumCost(long price[], int n)    {             // Sort the price array        Arrays.sort(price);                 long totalCost = 0;             // Calculate minimum price        // of n-2 most costly person        for (int i = n - 1; i > 1; i -= 2)        {            if (i == 2)            {                totalCost += price[2] + price[0];            }            else            {                     // Both the ways as discussed above                long price_first = price[i] + price[0] + 2 * price[1];                long price_second = price[i] + price[i - 1] + 2 * price[0];                totalCost += Math.min(price_first, price_second);            }        }             // Calculate the minimum price        // of the two cheapest person        if (n == 1)        {            totalCost += price[0];        }        else        {            totalCost += price[1];        }             return totalCost;    }         // Driver code    public static void main (String[] args)    {        long price[] = { 30, 40, 60, 70 };        int n = price.length;             System.out.println(minimumCost(price, n));    }}Â
// This code is contributed by AnkitRai01 |
Python
# Python3 implementation of the approachÂ
# Function to return the minimum costdef minimumCost(price, n):Â
    # Sort the price array    price = sorted(price)    totalCost = 0Â
    # Calculate minimum price    # of n-2 most costly person    for i in range(n - 1, 1, -2):        if (i == 2):            totalCost += price[2] + price[0]Â
        else:Â
            # Both the ways as discussed above            price_first = price[i] + price[0] + 2 * price[1]            price_second = price[i] + price[i - 1] + 2 * price[0]            totalCost += min(price_first, price_second)Â
    # Calculate the minimum price    # of the two cheapest person    if (n == 1):        totalCost += price[0]Â
    else:        totalCost += price[1]Â
    return totalCostÂ
# Driver codeÂ
price = [30, 40, 60, 70]n = len(price)Â
print(minimumCost(price, n))Â
# This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approachusing System;Â
class GFG{         // Function to return the minimum cost    static long minimumCost(long []price, int n)    {             // Sort the price array        Array.Sort(price);                 long totalCost = 0;             // Calculate minimum price        // of n-2 most costly person        for (int i = n - 1; i > 1; i -= 2)        {            if (i == 2)            {                totalCost += price[2] + price[0];            }            else            {                     // Both the ways as discussed above                long price_first = price[i] + price[0] + 2 * price[1];                long price_second = price[i] + price[i - 1] + 2 * price[0];                totalCost += Math.Min(price_first, price_second);            }        }             // Calculate the minimum price        // of the two cheapest person        if (n == 1)        {            totalCost += price[0];        }        else        {            totalCost += price[1];        }             return totalCost;    }         // Driver code    public static void Main ()    {        long []price = { 30, 40, 60, 70 };        int n = price.Length;             Console.WriteLine(minimumCost(price, n));    }}Â
// This code is contributed by AnkitRai01 |
Javascript
<script>    // Javascript implementation of the approach         // Function to return the minimum cost    function minimumCost(price, n)    {               // Sort the price array        price.sort();                   let totalCost = 0;               // Calculate minimum price        // of n-2 most costly person        for (let i = n - 1; i > 1; i -= 2)        {            if (i == 2)            {                totalCost += price[2] + price[0];            }            else            {                       // Both the ways as discussed above                let price_first = price[i] + price[0] + 2 * price[1];                let price_second = price[i] + price[i - 1] + 2 * price[0];                totalCost += Math.min(price_first, price_second);            }        }               // Calculate the minimum price        // of the two cheapest person        if (n == 1)        {            totalCost += price[0];        }        else        {            totalCost += price[1];        }               return totalCost;    }         let price = [ 30, 40, 60, 70 ];    let n = price.length;Â
    document.write(minimumCost(price, n));             </script> |
220
Â
Time Complexity: O(n * log n)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
