Connect N Ropes With Minimum Cost
You have been given 'N' ropes of different lengths, we need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. We need to connect the ropes with minimum cost.
Input format :
The first line of input contains an integer value 'N'. It denotes the total number of ropes.
The second line of input contains 'N' single space separated integers representing length of each rope i.e. a1, a2, a3, ... an.
Output Format :
The only line of output will contain an integer, the minimum cost for connecting all the ropes.
Note:
You need not to print anything, it has been already taken care of. Just implement the given function.
Constraints :
1 <= N <= 10^5
1 <= ai <= 10^9
Time Limit : 1 sec
AnswerBot
1y
The problem is to connect N ropes of different lengths into one rope with minimum cost.
Sort the array of rope lengths in ascending order.
Initialize a variable to keep track of the total cost.
While the...read more
Help your peers!
Add answer anonymously...
Top Goldman Sachs Software Engineer Intern interview questions & answers
Popular interview questions of Software Engineer Intern
Top HR questions asked in Goldman Sachs Software Engineer Intern
>
Goldman Sachs Software Engineer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app