Minimum cost of passing N persons through a given tunnel
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++
#include "bits/stdc++.h"
using namespace std;
int minimumCost( int arr[], int N, int H,
int X, int Y)
{
int cost = 0;
sort(arr, arr + N);
int i = 0, j = N - 1;
while (i < j) {
if (arr[i] + arr[j] < H) {
cost += Y;
i++;
j--;
}
else {
cost += X;
j--;
}
}
if (i == j)
cost += X;
return min(cost, N * X);
}
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
import java.io.*;
import java.util.Arrays;
class GFG{
public static int minimumCost( int arr[], int N, int H,
int X, int Y)
{
int cost = 0 ;
Arrays.sort(arr);
int i = 0 , j = N - 1 ;
while (i < j)
{
if (arr[i] + arr[j] < H)
{
cost += Y;
i++;
j--;
}
else
{
cost += X;
j--;
}
}
if (i == j)
cost += X;
return Math.min(cost, N * X);
}
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));
}
}
|
Python3
def minimumCost(arr, N, H, X, Y):
cost = 0
arr.sort()
i = 0
j = N - 1
while (i < j):
if (arr[i] + arr[j] < H):
cost + = Y
i + = 1
j - = 1
else :
cost + = X
j - = 1
if (i = = j):
cost + = X
return min (cost, N * X)
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))
|
C#
using System;
class GFG {
static int minimumCost( int [] arr, int N, int H, int X,
int Y)
{
int cost = 0;
Array.Sort(arr);
int i = 0, j = N - 1;
while (i < j) {
if (arr[i] + arr[j] < H) {
cost += Y;
i++;
j--;
}
else {
cost += X;
j--;
}
}
if (i == j)
cost += X;
return Math.Min(cost, N * X);
}
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));
}
}
|
Javascript
<script>
function minimumCost(arr, N, H, X, Y)
{
let cost = 0;
arr.sort( function (a, b){ return a - b});
let i = 0, j = N - 1;
while (i < j)
{
if (arr[i] + arr[j] < H)
{
cost += Y;
i++;
j--;
}
else
{
cost += X;
j--;
}
}
if (i == j)
cost += X;
return Math.min(cost, N * X);
}
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));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Last Updated :
01 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...