Given an array wood[] of size N, representing the length of N pieces of wood and an integer K, at least K pieces of the same length need to be cut from the given wooden pieces. The task is to find the maximum possible length of these K wooden pieces that can be obtained.
Examples:
Input: wood[] = {5, 9, 7}, K = 3
Output: 5
Explanation:
Cut arr[0] = 5 = 5
Cut arr[1] = 9 = 5 + 4
Cut arr[2] = 5 = 5 + 2
Therefore, the maximum length that can be obtained by cutting the woods into 3 pieces is 5.Input: wood[] = {5, 9, 7}, K = 4
Output: 4
Explanation:
Cut arr[0] = 5 = 4 + 1
Cut arr[1] = 9 = 2 * 4 + 1
Cut arr[2] = 7 = 4 + 3
Therefore, the maximum length that can be obtained by cutting the woods into 4 pieces is 4.
Approach: The problem can be solved using a Binary search. Follow the steps below to solve the problem:
- Find the maximum element from the array wood[] and store it in a variable, say Max.
- The value of L must lie in the raneg [1, Max]. Therefore, apply binary search over the range [1, Max].
- Initialize two variables say, left = 1 and right = Max to store the range in which the value of L lies.
- Check if it is possible to cut the woods into K pieces with length of each piece equal to (left + right) / 2 or not. If found to be true, then update left = (left + right) / 2.
- Otherwise, update right = (left + right) / 2 .
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to check if it is possible to cut// woods into K pieces of length lenbool isValid(int wood[], int N, int len, int K){ // Stores count of pieces // having length equal to K int count = 0; // Traverse wood[] array for (int i = 0; i < N; i++) { // Update count count += wood[i] / len; } return count >= K;}// Function to find the maximum value of Lint findMaxLen(int wood[], int N, int K){ // Stores minimum possible of L int left = 1; // Stores maximum possible value of L int right = *max_element(wood, wood + N); // Apply binary search over // the range [left, right] while (left <= right) { // Stores mid value of // left and right int mid = left + (right - left) / 2; // If it is possible to cut woods // into K pieces having length // of each piece equal to mid if (isValid(wood, N, mid, K)) { // Update left left = mid + 1; } else { // Update right right = mid - 1; } } return right;}// Driver Codeint main(){ int wood[] = { 5, 9, 7 }; int N = sizeof(wood) / sizeof(wood[0]); int K = 4; cout << findMaxLen(wood, N, K);} |
Java
// Java program to implement// the above approachimport java.util.*;class GFG{// Function to check if it is possible// to cut woods into K pieces of // length lenstatic boolean isValid(int wood[], int N, int len, int K){ // Stores count of pieces // having length equal to K int count = 0; // Traverse wood[] array for(int i = 0; i < N; i++) { // Update count count += wood[i] / len; } return count >= K;}// Function to find the maximum value of Lstatic int findMaxLen(int wood[], int N, int K){ // Stores minimum possible of L int left = 1; // Stores maximum possible value of L int right = Arrays.stream(wood).max().getAsInt(); // Apply binary search over // the range [left, right] while (left <= right) { // Stores mid value of // left and right int mid = left + (right - left) / 2; // If it is possible to cut woods // into K pieces having length // of each piece equal to mid if (isValid(wood, N, mid, K)) { // Update left left = mid + 1; } else { // Update right right = mid - 1; } } return right;}// Driver Codepublic static void main(String[] args){ int wood[] = { 5, 9, 7 }; int N = wood.length; int K = 4; System.out.print(findMaxLen(wood, N, K));}}// This code is contributed by Princi Singh |
Python3
# Python3 program to implement# the above approach# Function to check if it is possible to # cut woods into K pieces of length lendef isValid(wood, N, len, K): # Stores count of pieces # having length equal to K count = 0 # Traverse wood[] array for i in range(N): # Update count count += wood[i] // len return (count >= K)# Function to find the maximum value of Ldef findMaxLen(wood, N, K): # Stores minimum possible of L left = 1 # Stores maximum possible value of L right = max(wood) # Apply binary search over # the range [left, right] while (left <= right): # Stores mid value of # left and right mid = left + (right - left) // 2 # If it is possible to cut woods # into K pieces having length # of each piece equal to mid if (isValid(wood, N, mid, K)): # Update left left = mid + 1 else: # Update right right = mid - 1 return right# Driver Codeif __name__ == '__main__': wood = [ 5, 9, 7 ] N = len(wood) K = 4 print(findMaxLen(wood, N, K))# This code is contributed by mohit kumar 29 |
C#
// C# program to implement// the above approachusing System;using System.Linq;class GFG{// Function to check if it is possible// to cut woods into K pieces of // length lenstatic bool isValid(int []wood, int N, int len, int K){ // Stores count of pieces // having length equal to K int count = 0; // Traverse wood[] array for(int i = 0; i < N; i++) { // Update count count += wood[i] / len; } return count >= K;}// Function to find the maximum// value of Lstatic int findMaxLen(int []wood, int N, int K){ // Stores minimum possible // of L int left = 1; // Stores maximum possible // value of L int right = wood.Max(); // Apply binary search over // the range [left, right] while (left <= right) { // Stores mid value of // left and right int mid = left + (right - left) / 2; // If it is possible to cut woods // into K pieces having length // of each piece equal to mid if (isValid(wood, N, mid, K)) { // Update left left = mid + 1; } else { // Update right right = mid - 1; } } return right;}// Driver Codepublic static void Main(String[] args){ int []wood = {5, 9, 7}; int N = wood.Length; int K = 4; Console.Write(findMaxLen(wood, N, K));}}// This code is contributed by shikhasingrajput |
4
Time Complexity: O(N * Log2M), where M is the maximum element of the given array
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.

