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] = 7 = 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 range [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++ 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 len 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 L int findMaxLen( int wood[], int N, int K)
{ // Stores minimum possible value 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 Code int main()
{ int wood[] = { 5, 9, 7 };
int N = sizeof (wood) / sizeof (wood[0]);
int K = 4;
cout << findMaxLen(wood, N, K);
} |
// Java program to implement // the above approach import java.util.*;
class GFG{
// Function to check if it is possible // to cut woods into K pieces of // length len static 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 L static int findMaxLen( int wood[], int N,
int K)
{ // Stores minimum possible value 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 Code public 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 program to implement # the above approach # Function to check if it is possible to # cut woods into K pieces of length len def 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 L def findMaxLen(wood, N, K):
# Stores minimum possible value 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 Code if __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# program to implement // the above approach using System;
using System.Linq;
class GFG{
// Function to check if it is possible // to cut woods into K pieces of // length len static 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 L static int findMaxLen( int []wood,
int N, int K)
{ // Stores minimum possible
// value 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 Code public 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 |
<script> // Javascript program to implement // the above approach // Function to check if it is possible
// to cut woods into K pieces of
// length len
function isValid(wood , N , len , K) {
// Stores count of pieces
// having length equal to K
var count = 0;
// Traverse wood array
for (i = 0; i < N; i++) {
// Update count
count += parseInt(wood[i] / len);
}
return count >= K;
}
// Function to find the maximum value of L
function findMaxLen(wood , N , K) {
// Stores minimum possible value of L
var left = 1;
// Stores maximum possible value of L
var right = Math.max.apply(Math,wood);
// Apply binary search over
// the range [left, right]
while (left <= right) {
// Stores mid value of
// left and right
var 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 Code
var wood = [ 5, 9, 7 ];
var N = wood.length;
var K = 4;
document.write(findMaxLen(wood, N, K));
// This code is contributed by todaysgaurav </script> |
4
Time Complexity: O(N * Log2M), where M is the maximum element of the given array
Auxiliary Space: O(1)