Maximize ropes of consecutive length possible by connecting given ropes | Function to find maximized count of ropes of consecutive length ; Stores the maximum count of ropes of consecutive length ; Sort the ropes by their length ; Traverse the array ; 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 a rope of size ( curSize + 1 ) cannot be obtained ; Input ; Function Call
function maxConsecutiveRopes ( ropes , N ) { let curSize = 0 ; ropes . sort ( ( a , b ) => a - b ) for ( let i = 0 ; i < N ; i ++ ) { if ( ropes [ i ] <= curSize + 1 ) { curSize = curSize + ropes [ i ] ; } else break ; } return curSize ; } let N = 5 ; let ropes = [ 1 , 2 , 7 , 1 , 1 ] ; document . write ( maxConsecutiveRopes ( ropes , N ) ) ;