Maximum length of all possible K equal length ropes generated by cutting N ropes | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSize ( int a [ ] , int k , int n ) { int low = 1 ; int high = * max_element ( a , a + n ) ; int ans = -1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int count = 0 ; for ( int c = 0 ; c < n ; c ++ ) { count += a / mid ; } if ( count >= k ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 9 } ; int K = 6 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( maximumSize ( arr , K , n ) ) ; }