Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Connect Ropes
MEDIUM
25 mins
9 upvotes
Sorting
Greedy
Heap
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Sorting
-
-
Greedy
-
-
Heap
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Connect Ropes

Contributed by
Himanshu Tanwar
Medium
yellow-spark
0/80
Avg time to solve 25 mins
Success Rate 75 %
Share
9 upvotes

Problem Statement

Given a number of ropes say ‘N’ and an array of integers of size ‘N’ containing the length of ropes. Your task is to connect the ropes into one. The cost to connect two ropes is equal to the sum of their lengths. Find the minimum cost for connecting all the ropes.

Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
4
4 3 2 6
3
1 1 8
Sample Output 1:
29
12
Explanation for Sample Input 1:
For test case 1
Optimal way to connect the ropes is:
Connecting 3 and 2 costs 5.
Ropes left  = [4,5,6]
Connecting 4 and 5 costs 9.
Ropes left = [9,6]
Connecting 9 and 6 costs 15.
One rope of length 15 is made.
Total cost for all the connections = 5 + 9 + 15 = 29.
Sample Input 2:
2
3
5 3 8
1
3
Sample Output 2:
24
0
Reset Code
Full screen
Auto
copy-code
Console