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

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 = 5Output:4Explanation:

Below are the possible cutting of the ropes:

- arr[0](= 5) is cutted into {4, 1}.
- arr[2](= 7) is cutted into {4, 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 = 6Output: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)**.

- Find the mid-value of the range
- 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 <bits/stdc++.h>` `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

`<script>` ` ` `// Javascript program for the above approach` ` ` `// Function to find the maximum size` ` ` `// of ropes having frequency at least` ` ` `// K by cutting the given ropes` ` ` `function` `maximumSize( a, k) {` ` ` `// Stores the left and` ` ` `// the right boundaries` ` ` `let low = 1;` ` ` `let high = Math.max.apply(Math, a);` ` ` ` ` `// Stores the maximum length` ` ` `// of rope possible` ` ` `let ans = -1;` ` ` `// Iterate while low is less` ` ` `// than or equal to high` ` ` `while` `(low <= high) {` ` ` `// Stores the mid value of` ` ` `// the range [low, high]` ` ` `let mid = Math.floor(low + (high - low) / 2);` ` ` ` ` `// Stores the count of ropes` ` ` `// of length mid` ` ` `let count = 0;` ` ` `// Traverse the array arr[]` ` ` `for` `(let c = 0;` ` ` `c < a.length; c++) {` ` ` `count += Math.floor(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` ` ` `let arr = [ 1, 2, 3, 4, 9 ];` ` ` `let K = 6;` ` ` `document.write(maximumSize(arr, K))` ` ` `// This code is contributed by Hritik` ` ` `</script>` |

**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**.