Close
Topic list
Connect Ropes
MEDIUM
25 mins
Sorting
Greedy
Heap
Topics (Covered in this problem)
Problem solved
Skill meter
Sorting
-
-
Greedy
-
-
Heap
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
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
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
0/80
Avg time to solve 25 mins
Success Rate 75 %
Share

## 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
``````
Auto
Console