Given two positive integers X and Y and an array arr[] consisting of N positive integers such that arr[i] represents the height of the ith person and there is a tunnel of height H, the task is to find the total minimum cost required to pass all the N persons through the given tunnel such at most two-person whose sum of heights is less than H can pass at a time according to the following rules:
- When two person passes through the tunnel at a time, then the cost is Y.
- When one person passes through the tunnel at a time, then the cost is X.
Note: All array elements are less than H.
Examples:
Input: arr[] = {1, 3, 4, 4, 2}, X = 4, Y = 6, H = 9
Output: 16
Explanation:
Consider the passing of persons according to the below order:
- Person 1 and Person 4 having heights 1 and 4 respectively has the sum of heights as 1 + 4 = 5 < H(= 9). Therefore, the cost for this operation is Y(= 6).
- Person 2 and Person 3 having heights 3 and 4 respectively has the sum of heights as 3 + 4 = 7 < H(= 9). Therefore, the cost for this operation is Y(= 6).
- Person 5 has height 3 which is less than H(= 9). Therefore, the cost for this operation is X( = 4).
Therefore, the total cost is 6 + 6 + 4 = 16, which is minimum among all possible combinations.
Input: arr[] = {1, 3, 4}, X = 4, Y = 6, H = 9
Output: 10
Approach: The given problem can be solved by using the Greedy Approach and using the Two Pointer Technique. The idea is to choose those two persons whose sum of the heights is less than H with the cost of Y. Otherwise, choose the maximum height person among the two-person and pass them into the tunnel with the cost of X. Follow the steps below to solve the problem:
- Sort the given array arr[] in increasing order.
- Initialize two pointers, say i and j as 0 and (N – 1) respectively to points to the extremities of the array.
- Iterate until the value of i is less than j and perform the following steps:
- If the sum of values of arr[i] and arr[j] is less than H, then incrementing the value of cost by Y and increment the value of i and decrement the value of j by 1.
- Otherwise, decrement the value of j by 1 and update the value of cost by X.
- If the value of i and j are equal then increment the value of cost by X.
- After completing the above steps, print the value of cost as the resultant minimum cost.
Below is the implementation of the above approach:
C++
// C++ program for the above approachÂ
#include "bits/stdc++.h"using namespace std;Â
// Function to find the minimum total// cost of passing at most two-person// at a time through the tunnelint minimumCost(int arr[], int N, int H,                int X, int Y){Â
    // Stores the resultant cost    int cost = 0;Â
    // Sort the given array    sort(arr, arr + N);Â
    // Initialize two pointers    int i = 0, j = N - 1;Â
    // Iterate until i is less than j    while (i < j) {Â
        // If the sum of values at        // i and j is less than H        if (arr[i] + arr[j] < H) {Â
            // Increment the cost            cost += Y;Â
            // Update the pointers            i++;            j--;        }Â
        // Otherwise        else {            cost += X;            j--;        }    }Â
    // If i and j points to the same    // element, then that person is    // not passed to the tunnel    if (i == j)        cost += X;Â
    // Return the minimum of the total    // cost and cost of passing all the    // person individually    return min(cost, N * X);}Â
// Driver Codeint main(){Â Â Â Â int arr[] = { 1, 3, 4, 4, 2 };Â Â Â Â int X = 4, Y = 6, H = 9;Â Â Â Â int N = sizeof(arr) / sizeof(arr[0]);Â Â Â Â cout << minimumCost(arr, N, H, X, Y);Â
    return 0;} |
Java
// Java program for the above approach import java.io.*;import java.util.Arrays;Â
class GFG{     // Function to find the minimum total// cost of passing at most two-person// at a time through the tunnelpublic static int minimumCost(int arr[], int N, int H,                              int X, int Y){         // Stores the resultant cost    int cost = 0;Â
    // Sort the given array    Arrays.sort(arr);Â
    // Initialize two pointers    int i = 0, j = N - 1;Â
    // Iterate until i is less than j    while (i < j)     {                 // If the sum of values at        // i and j is less than H        if (arr[i] + arr[j] < H)         {                         // Increment the cost            cost += Y;Â
            // Update the pointers            i++;            j--;        }Â
        // Otherwise        else        {            cost += X;            j--;        }    }Â
    // If i and j points to the same    // element, then that person is    // not passed to the tunnel    if (i == j)        cost += X;Â
    // Return the minimum of the total    // cost and cost of passing all the    // person individually    return Math.min(cost, N * X);}Â
// Driver codepublic static void main(String[] args){Â Â Â Â int arr[] = { 1, 3, 4, 4, 2 };Â Â Â Â int X = 4, Y = 6, H = 9;Â Â Â Â int N = arr.length;Â
    System.out.println(minimumCost(arr, N, H, X, Y));}}Â
// This code is contributed by Potta Lokesh |
Python3
# Python 3 program for the above approachÂ
# Function to find the minimum total# cost of passing at most two-person# at a time through the tunneldef minimumCost(arr, N, H, X, Y):       # Stores the resultant cost    cost = 0Â
    # Sort the given array    arr.sort()Â
    # Initialize two pointers    i = 0    j = N - 1Â
    # Iterate until i is less than j    while (i < j):Â
        # If the sum of values at        # i and j is less than H        if (arr[i] + arr[j] < H):Â
            # Increment the cost            cost += YÂ
            # Update the pointers            i += 1            j -= 1Â
        # Otherwise        else:            cost += X            j -= 1Â
    # If i and j points to the same    # element, then that person is    # not passed to the tunnel    if (i == j):        cost += XÂ
    # Return the minimum of the total    # cost and cost of passing all the    # person individually    return min(cost, N * X)Â
# Driver Codeif __name__ == '__main__':Â Â Â Â arr = [1, 3, 4, 4, 2]Â Â Â Â X = 4Â Â Â Â Y = 6Â Â Â Â H = 9Â Â Â Â N = len(arr)Â Â Â Â print(minimumCost(arr, N, H, X, Y))Â Â Â Â Â Â Â Â Â # This code is contributed by bgangwar59. |
C#
// C# program for the above approachusing System;class GFG {Â
    // Function to find the minimum total    // cost of passing at most two-person    // at a time through the tunnel    static int minimumCost(int[] arr, int N, int H, int X,                           int Y)    {Â
        // Stores the resultant cost        int cost = 0;Â
        // Sort the given array        Array.Sort(arr);Â
        // Initialize two pointers        int i = 0, j = N - 1;Â
        // Iterate until i is less than j        while (i < j) {Â
            // If the sum of values at            // i and j is less than H            if (arr[i] + arr[j] < H) {Â
                // Increment the cost                cost += Y;Â
                // Update the pointers                i++;                j--;            }Â
            // Otherwise            else {                cost += X;                j--;            }        }Â
        // If i and j points to the same        // element, then that person is        // not passed to the tunnel        if (i == j)            cost += X;Â
        // Return the minimum of the total        // cost and cost of passing all the        // person individually        return Math.Min(cost, N * X);    }Â
    // Driver code    public static void Main()    {        int[] arr = { 1, 3, 4, 4, 2 };        int X = 4, Y = 6, H = 9;        int N = arr.Length;Â
        Console.WriteLine(minimumCost(arr, N, H, X, Y));    }}Â
// This code is contributed by subhammahato348. |
Javascript
<script>Â
// JavaScript program for the above approachÂ
// Function to find the minimum total// cost of passing at most two-person// at a time through the tunnelfunction minimumCost(arr, N, H, X, Y){         // Stores the resultant cost    let cost = 0;Â
    // Sort the given array    arr.sort(function(a, b){return a - b});         // Initialize two pointers    let i = 0, j = N - 1;Â
    // Iterate until i is less than j    while (i < j)    {                 // If the sum of values at        // i and j is less than H        if (arr[i] + arr[j] < H)        {                         // Increment the cost            cost += Y;Â
            // Update the pointers            i++;            j--;        }Â
        // Otherwise        else        {            cost += X;            j--;        }    }Â
    // If i and j points to the same    // element, then that person is    // not passed to the tunnel    if (i == j)        cost += X;Â
    // Return the minimum of the total    // cost and cost of passing all the    // person individually    return Math.min(cost, N * X);}Â
// Driver Codelet arr = [ 1, 3, 4, 4, 2 ];let X = 4, Y = 6, H = 9;let N = arr.length;Â
document.write(minimumCost(arr, N, H, X, Y));Â
// This code is contributed by Potta LokeshÂ
</script> |
16
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!
