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++
#include <bits/stdc++.h>
using namespace std;
bool isValid(int wood[], int N, int len, int K)
{
int count = 0;
for (int i = 0; i < N; i++) {
count += wood[i] / len;
}
return count >= K;
}
int findMaxLen(int wood[], int N, int K)
{
int left = 1;
int right = *max_element(wood,
wood + N);
while (left <= right) {
int mid = left + (right - left) / 2;
if (isValid(wood, N, mid,
K)) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return right;
}
int main()
{
int wood[] = { 5, 9, 7 };
int N = sizeof(wood) / sizeof(wood[0]);
int K = 4;
cout << findMaxLen(wood, N, K);
}
Java
import java.util.*;
class GFG{
static boolean isValid(int wood[], int N,
int len, int K)
{
int count = 0;
for(int i = 0; i < N; i++)
{
count += wood[i] / len;
}
return count >= K;
}
static int findMaxLen(int wood[], int N,
int K)
{
int left = 1;
int right = Arrays.stream(wood).max().getAsInt();
while (left <= right)
{
int mid = left + (right - left) / 2;
if (isValid(wood, N, mid, K))
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return right;
}
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));
}
}
Python3
def isValid(wood, N, len, K):
count = 0
for i in range(N):
count += wood[i] // len
return (count >= K)
def findMaxLen(wood, N, K):
left = 1
right = max(wood)
while (left <= right):
mid = left + (right - left) // 2
if (isValid(wood, N, mid, K)):
left = mid + 1
else:
right = mid - 1
return right
if __name__ == '__main__':
wood = [ 5, 9, 7 ]
N = len(wood)
K = 4
print(findMaxLen(wood, N, K))
C#
using System;
using System.Linq;
class GFG{
static bool isValid(int []wood, int N,
int len, int K)
{
int count = 0;
for(int i = 0; i < N; i++)
{
count += wood[i] / len;
}
return count >= K;
}
static int findMaxLen(int []wood,
int N, int K)
{
int left = 1;
int right = wood.Max();
while (left <= right)
{
int mid = left +
(right - left) / 2;
if (isValid(wood, N,
mid, K))
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return right;
}
public static void Main(String[] args)
{
int []wood = {5, 9, 7};
int N = wood.Length;
int K = 4;
Console.Write(findMaxLen(wood,
N, K));
}
}
Javascript
<script>
function isValid(wood , N , len , K) {
var count = 0;
for (i = 0; i < N; i++) {
count += parseInt(wood[i] / len);
}
return count >= K;
}
function findMaxLen(wood , N , K) {
var left = 1;
var right = Math.max.apply(Math,wood);
while (left <= right) {
var mid = left + (right - left) / 2;
if (isValid(wood, N, mid, K)) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return right;
}
var wood = [ 5, 9, 7 ];
var N = wood.length;
var K = 4;
document.write(findMaxLen(wood, N, K));
</script>
Time Complexity: O(N * Log2 M), where M is the maximum element of the given arrayAuxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...