Maximize ropes of consecutive length possible by connecting given ropes | Java program for the above approach ; 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 ; Driver code ; Input ; Function Call
import java . util . * ; class GFG { static int maxConsecutiveRope ( int ropes [ ] , int N ) { int curSize = 0 ; Arrays . sort ( ropes ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ropes [ i ] <= curSize + 1 ) { curSize = curSize + ropes [ i ] ; } else break ; } return curSize ; } public static void main ( String [ ] args ) { int N = 5 ; int ropes [ ] = { 1 , 2 , 7 , 1 , 1 } ; System . out . println ( maxConsecutiveRope ( ropes , N ) ) ; } }