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

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 = 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}.
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 [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++
Java
Python3
C#
Javascript
// 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 ropesint 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 Codeint 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 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));    }}
# Python program for the above approach # Function to find the maximum size# of ropes having frequency at least# K by cutting the given ropesdef 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 Codeif __name__ == '__main__':    arr = [1, 2, 3, 4, 9]    K = 6    print(maximumSize(arr, K)) # This code is contributed by mohit kumar 29.
// C# program for the above approachusing System;using System.Linq; class GFG{ // Function to find the maximum size// of ropes having frequency at least// K by cutting the given ropesstatic 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 Codepublic 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
<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>
2
 
Time Complexity: O(N * log N)Auxiliary Space: O(1)
mohit kumar 29
ukasp
29AjayKumar
hritikrommie
Amazon
Binary Search
interview-preparation
Arrays
Greedy
Mathematical
Amazon
Arrays
Greedy
Mathematical
Binary Search
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Write a program to print all permutations of a given string
Huffman Coding | Greedy Algo-3