Challenge: Connecting 'n' Pipes with Minimum Cost

Given “n” block chains of varying lengths, find the minimum cost of connecting them.

What is a Blockchain?

Problem statement

Given n pipes of different lengths, implement a function that connects these pipes into one pipe. The cost to connect two pipes is equal to the sum of their lengths. We need to connect the pipes with minimum cost.

Input

The input is an int array where its length is equal to the number of pipes available and its indices are the specific lengths of the pipes.

Output

The output is the total cost of connecting the pipes.

Sample input

int [] pipes = {4, 2, 3, 7};  //it means there are a total of 4 pipes 
                             // having lengths of 4, 2, 3 & 7 consecutively

Sample output

cost = 30;

Note that connecting the pipes in s different order results in a different cost.

Coding exercise

Take a close look and design a step-by-step algorithm before jumping on to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the hint and solution provided in the code tab. Good Luck!

Press + to interact
class MinimumCost
{
public static int minCost(int[] pipes)
{
int cost = -1;
return cost;
}
}