Open In App

Minimum cost of passing N persons through a given tunnel

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. 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).
  2. 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).
  3. 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 tunnel
int 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 Code
int 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 tunnel
public 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 code
public 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 tunnel
def 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 Code
if __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 approach
using 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 tunnel
function 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 Code
let 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>


Output

16

Time Complexity: O(N*log N)
Auxiliary Space: O(1)



Last Updated : 01 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads