Maximum length of all possible K equal length ropes generated by cutting N ropes | Java program for the above approach ; Function to find the maximum size of ropes having frequency at least K by cutting the given ropes ; Stores the left and the right boundaries ; Stores the maximum length of rope possible ; Iterate while low is less than or equal to high ; Stores the mid value of the range [ low , high ] ; Stores the count of ropes of length mid ; Traverse the array arr [ ] ; If count is at least K ; Assign mid to ans ; Update the value of low ; Otherwise , update the value of high ; Return the value of ans ; Driver Code
import java . util . * ; class GFG { static int maximumSize ( Integer [ ] a , int k ) { int low = 1 ; int high = Collections . max ( Arrays . asList ( a ) ) ; int ans = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int count = 0 ; for ( int c = 0 ; c < a . length ; c ++ ) { count += a / mid ; } if ( count >= k ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 1 , 2 , 3 , 4 , 9 } ; int K = 6 ; System . out . println ( maximumSize ( arr , K ) ) ; } }