 Open in App
Not now

# Maximize ropes of consecutive length possible by connecting given ropes

• Last Updated : 26 May, 2021

Given an array A[ ] of size N where each array element represents the length of a rope, the task is to find the number of ropes of consecutive length that can be created by connecting given ropes starting from length 1.

Examples :

Input: N = 5, A[ ] = {1, 2, 7, 1, 1}

Output:
Explanation:
Length | Ropes
1 | 
2 | [1, 1]
3 | [1, 2]
4 | [1, 2, 1]
5 | [1, 2, 1, 1]

Input N = 5, A = {1, 3, 2, 4, 2}
Output: 12

Approach: This problem can be solved by using the fact that if we are able to create ropes of range [1, K] lengths and there is a rope left of length L such that (L <= K+1) then we can create ropes of range [1, K+L] by adding L length rope to each rope of the range [max(1, K-L+1), K]. So to solve the problem, first, sort the array and then traverse the array and check each time if the current element is less than or equal to the maximum consecutive length we have obtained + 1. If found to be true, add that element to maximum consecutive length. Otherwise, return the answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find maximized count``// of ropes of consecutive length``int` `maxConsecutiveRopes(``int` `ropes[], ``int` `N)``{``    ``// Stores the maximum count``    ``// of ropes of consecutive length``    ``int` `curSize = 0;` `    ``// Sort the ropes by their length``    ``sort(ropes, ropes + N);` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// If size of the current rope is less``        ``// than or equal to current maximum``        ``// possible size + 1, update the``        ``// range to curSize + ropes[i]` `        ``if` `(ropes[i] <= curSize + 1) {``            ``curSize = curSize + ropes[i];``        ``}` `        ``// If a rope of size (curSize + 1)``        ``// cannot be obtained``        ``else``            ``break``;``    ``}``    ``return` `curSize;``}` `// Driver Code``int` `main()``{``    ``// Input``    ``int` `N = 5;``    ``int` `ropes[] = { 1, 2, 7, 1, 1 };` `    ``// Function Call``    ``cout << maxConsecutiveRopes(ropes, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG {` `    ``// Function to find maximized count``    ``// of ropes of consecutive length``    ``static` `int` `maxConsecutiveRope(``int` `ropes[], ``int` `N)``    ``{``        ``// Stores the maximum count``        ``// of ropes of consecutive length``        ``int` `curSize = ``0``;` `        ``// Sort the ropes by their length``        ``Arrays.sort(ropes);` `        ``// Traverse the array``        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``// If size of the current rope is less``            ``// than or equal to current maximum``            ``// possible size + 1, update the``            ``// range to curSize + ropes[i]` `            ``if` `(ropes[i] <= curSize + ``1``) {``                ``curSize = curSize + ropes[i];``            ``}` `            ``// If a rope of size (curSize + 1)``            ``// cannot be obtained``            ``else``                ``break``;``        ``}``        ``return` `curSize;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``// Input``        ``int` `N = ``5``;``        ``int` `ropes[] = { ``1``, ``2``, ``7``, ``1``, ``1` `};` `        ``// Function Call``        ``System.out.println(``            ``maxConsecutiveRope(ropes, N));``    ``}``}`

## Python3

 `# Python3 program for the above approach` `# Function to find maximized count``# of ropes of consecutive length``def` `maxConsecutiveRopes(ropes, N):` `    ``# Stores the maximum count``    ``# of ropes of consecutive length``    ``curSize ``=` `0` `    ``# Sort the ropes by their length``    ``ropes ``=` `sorted``(ropes)` `    ``# Traverse the array``    ``for` `i ``in` `range``(N):``        ` `        ``# If size of the current rope is less``        ``# than or equal to current maximum``        ``# possible size + 1, update the``        ``# range to curSize + ropes[i]``        ``if` `(ropes[i] <``=` `curSize ``+` `1``):``            ``curSize ``=` `curSize ``+` `ropes[i]` `        ``# If a rope of size (curSize + 1)``        ``# cannot be obtained``        ``else``:``            ``break` `    ``return` `curSize` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Input``    ``N ``=` `5``    ``ropes ``=` `[ ``1``, ``2``, ``7``, ``1``, ``1` `]` `    ``# Function Call``    ``print` `(maxConsecutiveRopes(ropes, N))``    ` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{``    ` `// Function to find maximized count``// of ropes of consecutive length``static` `int` `maxConsecutiveRope(``int``[] ropes, ``int` `N)``{``    ` `    ``// Stores the maximum count``    ``// of ropes of consecutive length``    ``int` `curSize = 0;` `    ``// Sort the ropes by their length``    ``Array.Sort(ropes);` `    ``// Traverse the array``    ``for``(``int` `i = 0; i < N; i++)``    ``{``        ` `        ``// If size of the current rope is less``        ``// than or equal to current maximum``        ``// possible size + 1, update the``        ``// range to curSize + ropes[i]``        ``if` `(ropes[i] <= curSize + 1)``        ``{``            ``curSize = curSize + ropes[i];``        ``}` `        ``// If a rope of size (curSize + 1)``        ``// cannot be obtained``        ``else``            ``break``;``    ``}``    ``return` `curSize;``}` `// Driver code``public` `static` `void` `Main ()``{``    ` `    ``// Input``    ``int` `N = 5;``    ``int``[] ropes = { 1, 2, 7, 1, 1 };` `    ``// Function Call``    ``Console.WriteLine(maxConsecutiveRope(ropes, N));``}``}` `// This code is contributed by souravghosh0416`

## Javascript

 ``

Output:

`5`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up