Related Articles

# Maximum length of all possible K equal length ropes generated by cutting N ropes

• Difficulty Level : Easy
• Last Updated : 15 Jun, 2021

Given an array arr[] consisting of N positive integers representing the lengths of N ropes and a positive integer K, the task is to find the maximum length of the rope that has a frequency of at least K by cutting any ropes in any number of pieces.

Examples:

Input: arr[] = {5, 2, 7, 4, 9}, K = 5
Output: 4
Explanation:
Below are the possible cutting of the ropes:

1. arr[0](= 5) is cutted into {4, 1}.
2. arr[2](= 7) is cutted into {4, 3}.
3. arr[4](= 9) is cutted into {4, 4, 1}.

After the above combinations of cuts, the maximum length is 4, which is of frequency at least(K = 5).

Input: arr[] = {1, 2, 3, 4, 9}, K = 6
Output: 2

Approach: The given problem can be solved by using the Binary Search. Follow the steps below to solve the problem:

• Initialize 3 variables, say low as 1, high as the maximum value of array arr[], and ans as -1, to store the left boundary right boundary for the binary search and to store the maximum possible length of K ropes.
• Iterate until low is less than or equal to high and perform the following steps:
• Find the mid-value of the range [low, high] and store it in a variable say mid.
• Traverse the array arr[] and find the count of ropes of length mid that can be obtained by cutting the ropes and store it in a variable say, count.
• If the value of count is at least K, then update the value of mid as ans and update the value of low as (mid + 1).
• Otherwise, update the value of high as (mid – 1).
• After completing the steps, print the value of ans as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the maximum size``// of ropes having frequency at least``// K by cutting the given ropes``int` `maximumSize(``int` `a[], ``int` `k, ``int` `n)``{``    ``// Stores the left and``    ``// the right boundaries``    ``int` `low = 1;``    ``int` `high = *max_element(a, a + n);` `    ``// Stores the maximum length``    ``// of rope possible``    ``int` `ans = -1;` `    ``// Iterate while low is less``    ``// than or equal to high``    ``while` `(low <= high) {` `        ``// Stores the mid value of``        ``// the range [low, high]``        ``int` `mid = low + (high - low) / 2;` `        ``// Stores the count of ropes``        ``// of length mid``        ``int` `count = 0;` `        ``// Traverse the array arr[]``        ``for` `(``int` `c = 0; c < n; c++) {``            ``count += a / mid;``        ``}` `        ``// If count is at least K``        ``if` `(count >= k) {` `            ``// Assign mid to ans``            ``ans = mid;` `            ``// Update the value``            ``// of low``            ``low = mid + 1;``        ``}` `        ``// Otherwise, update the``        ``// value of high``        ``else` `{``            ``high = mid - 1;``        ``}``    ``}` `    ``// Return the value of ans``    ``return` `ans;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 4, 9 };``    ``int` `K = 6;``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``cout << (maximumSize(arr, K, n));``}` `// This code is contributed by ukasp`

## Java

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

## Python3

 `# Python program for the above approach` `# Function to find the maximum size``# of ropes having frequency at least``# K by cutting the given ropes``def` `maximumSize(a, k):``  ` `    ``# Stores the left and``    ``# the right boundaries``    ``low ``=` `1``    ``high ``=` `max``(a)` `    ``# Stores the maximum length``    ``# of rope possible``    ``ans ``=` `-``1` `    ``# Iterate while low is less``    ``# than or equal to high``    ``while` `(low <``=` `high):` `        ``# Stores the mid value of``        ``# the range [low, high]``        ``mid ``=` `low ``+` `(high ``-` `low) ``/``/` `2` `        ``# Stores the count of ropes``        ``# of length mid``        ``count ``=` `0` `        ``# Traverse the array arr[]``        ``for` `c ``in` `range``(``len``(a)):``            ``count ``+``=` `a ``/``/` `mid`  `        ``# If count is at least K``        ``if` `(count >``=` `k):` `            ``# Assign mid to ans``            ``ans ``=` `mid` `            ``# Update the value``            ``# of low``            ``low ``=` `mid ``+` `1` `        ``# Otherwise, update the``        ``# value of high``        ``else``:``            ``high ``=` `mid ``-` `1` `    ``# Return the value of ans``    ``return` `ans` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``1``, ``2``, ``3``, ``4``, ``9``]``    ``K ``=` `6``    ``print``(maximumSize(arr, K))` `# This code is contributed by mohit kumar 29.`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Linq;` `class` `GFG{` `// Function to find the maximum size``// of ropes having frequency at least``// K by cutting the given ropes``static` `int` `maximumSize(``int``[] a, ``int` `k)``{``    ` `    ``// Stores the left and``    ``// the right boundaries``    ``int` `low = 1;``    ``int` `high = a.Max();` `    ``// Stores the maximum length``    ``// of rope possible``    ``int` `ans = -1;` `    ``// Iterate while low is less``    ``// than or equal to high``    ``while` `(low <= high)``    ``{``        ` `        ``// Stores the mid value of``        ``// the range [low, high]``        ``int` `mid = low + (high - low) / 2;` `        ``// Stores the count of ropes``        ``// of length mid``        ``int` `count = 0;` `        ``// Traverse the array []arr``        ``for``(``int` `c = 0;``                ``c < a.Length; c++)``        ``{``            ``count += a / mid;``        ``}` `        ``// If count is at least K``        ``if` `(count >= k)``        ``{``            ` `            ``// Assign mid to ans``            ``ans = mid;` `            ``// Update the value``            ``// of low``            ``low = mid + 1;``        ``}` `        ``// Otherwise, update the``        ``// value of high``        ``else``        ``{``            ``high = mid - 1;``        ``}``    ``}` `    ``// Return the value of ans``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int``[] arr = { 1, 2, 3, 4, 9 };``    ``int` `K = 6;``    ` `    ``Console.WriteLine(``        ``maximumSize(arr, K));``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``
Output:
`2`

Time Complexity: O(N * log N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up