Given N rods of different lengths. The task is to cut all the rods with some maximum integer height ‘h’ such that the sum of cut-off lengths of the rod is maximized and must be greater than M. Print -1 if no such cut is possible.
Note: A rod cannot be cut also.
Examples:
Input: N = 7, M = 8, a[] = {1, 2, 3, 5, 4, 7, 6}
Output: 3
Rod 1 and 2 are untouched, and rod 3, 4, 5, 6, 7 are cut with the cut-off lengths being (3-3) + (4-3) + (5-3) + (7-3) + (6-3) which is equal to 10 which is greater than M = 8.
Input: N = 4, M = 2, a[] = {1, 2, 3, 3}
Output: 2
Approach:
- Sort the array in ascending order
- Run a binary search with values low=0 and high=length[n-1], such that mid=(low+high)/2.
- Run a loop from n-1 till 0 adding the height of the rod cut-off to the sum.
- If the sum is greater than or equal to m, assign low with mid+1 otherwise high will be updated with mid.
- After Binary search is completed the answer will be low-1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int binarySearch( int adj[], int target, int length)
{
int low = 0;
int high = adj[length - 1];
while (low < high) {
int f = 0, sum = 0;
int mid = low + (high - low) / 2;
for ( int i = length - 1; i >= 0; i--) {
if (adj[i] > mid) {
sum = sum + adj[i] - mid;
}
if (sum >= target) {
f = 1;
low = mid + 1;
break ;
}
}
if (f == 0)
high = mid;
}
return low - 1;
}
int main()
{
int n1 = 7;
int n2 = 8;
int adj[] = { 1, 2, 3, 4, 5, 7, 6 };
sort(adj, adj + n1);
cout << binarySearch(adj, n2, n1);
}
|
Java
import java.util.*;
class GFG
{
static int binarySearch( int adj[],
int target,
int length)
{
int low = 0 ;
int high = adj[length - 1 ];
while (low < high)
{
int f = 0 , sum = 0 ;
int mid = low + (high - low) / 2 ;
for ( int i = length - 1 ;
i >= 0 ; i--)
{
if (adj[i] > mid)
{
sum = sum + adj[i] - mid;
}
if (sum >= target)
{
f = 1 ;
low = mid + 1 ;
break ;
}
}
if (f == 0 )
high = mid;
}
return low - 1 ;
}
public static void main(String args[])
{
int n1 = 7 ;
int n2 = 8 ;
int adj[] = { 1 , 2 , 3 , 4 , 5 , 7 , 6 };
Arrays.sort(adj);
System.out.println(binarySearch(adj, n2, n1));
}
}
|
Python3
def binarySearch(adj, target, length) :
low = 0
high = adj[length - 1 ]
while (low < high) :
f, sum = 0 , 0
mid = low + (high - low) / / 2 ;
for i in range (length - 1 , - 1 , - 1 ) :
if adj[i] > mid :
sum = sum + adj[i] - mid
if sum > = target :
f = 1
low = mid + 1
break
if f = = 0 :
high = mid
return low - 1
if __name__ = = "__main__" :
n1 = 7
n2 = 8
adj = [ 1 , 2 , 3 , 4 , 5 , 7 , 6 ]
adj.sort()
print (binarySearch(adj, n2, n1))
|
C#
using System;
class GFG
{
static int binarySearch( int []adj,
int target,
int length)
{
int low = 0;
int high = adj[length - 1];
while (low < high)
{
int f = 0, sum = 0;
int mid = low + (high - low) / 2;
for ( int i = length - 1;
i >= 0; i--)
{
if (adj[i] > mid)
{
sum = sum + adj[i] - mid;
}
if (sum >= target)
{
f = 1;
low = mid + 1;
break ;
}
}
if (f == 0)
high = mid;
}
return low - 1;
}
public static void Main()
{
int n1 = 7;
int n2 = 8;
int []adj = {1, 2, 3, 4, 5, 7, 6};
Array.Sort(adj);
Console.WriteLine(binarySearch(adj, n2, n1));
}
}
|
PHP
<?php
function binarySearch(& $adj , $target ,
$length )
{
$low = 0;
$high = $adj [ $length - 1];
while ( $low < $high )
{
$f = 0;
$sum = 0;
$mid = $low + ( $high - $low ) / 2;
for ( $i = $length - 1; $i >= 0; $i --)
{
if ( $adj [ $i ] > $mid )
{
$sum = $sum + $adj [ $i ] - $mid ;
}
if ( $sum >= $target )
{
$f = 1;
$low = $mid + 1;
break ;
}
}
if ( $f == 0)
$high = $mid ;
}
return $low - 1;
}
$n1 = 7;
$n2 = 8;
$adj = array ( 1, 2, 3, 4, 5, 7, 6 );
sort( $adj );
echo (int)binarySearch( $adj , $n2 , $n1 );
?>
|
Javascript
<script>
function binarySearch(adj,target,length)
{
let low = 0;
let high = adj[length - 1];
while (low < high)
{
let f = 0, sum = 0;
let mid = low + Math.floor((high - low) / 2);
for (let i = length - 1;
i >= 0; i--)
{
if (adj[i] > mid)
{
sum = sum + adj[i] - mid;
}
if (sum >= target)
{
f = 1;
low = mid + 1;
break ;
}
}
if (f == 0)
high = mid;
}
return low - 1;
}
let n1 = 7;
let n2 = 8;
let adj=[1, 2, 3, 4, 5, 7, 6 ];
adj.sort( function (a,b){ return a-b;});
document.write(binarySearch(adj, n2, n1));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)