Given an arrayA[ ] 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.
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:
Please Login to comment...