Maximum volume of cube for every person when edge of N cubes are given
Given an array of N integers which denotes the edges of N cubical structures respectively. Also given are M integers which denote the number of peoples. The task is to find the maximum amount of volume of a cube that can be given to every person.
Note: Cubes can be cut off any shape from any of the N cubes.
Examples:
Input: a[] = {1, 1, 1, 2, 2}, m = 3
Output: 4
All three people get a slice of volume 4 each
Person 1 gets a slice of volume 4 from the last cube.
Person 2 gets a slice of volume 4 from the last cube.
Person 3 gets a slice of volume 4 from the second last cube.
Input: a[] = {2, 2, 2, 2, 2}, m = 4
Output: 8
Naive Approach: A naive approach is to first calculate the volume of all the cubes and then linearly check for every volume that it can be distributed among all M people or not and find the maximum volume among all such volumes.
Time Complexity: O(N2)
Efficient Approach: An efficient approach is to use binary search to find the answer. Since the edge lengths are given in the array, convert them to the volume of the respective cubes. Find the maximum volume among the volumes of all the cubes. Say, the maximum volume is maxVolume. Now, perform binary search on the range [0, maxVolume].
- Calculate the middle value of the range, say mid.
- Now, calculate the total number of cubes that can be cut of all the cubes of volume mid.
- If the total cubes that can be cut exceed the number of persons, then that amount of volume of cubes can be cut for every person, hence we check for a larger value in the range [mid+1, maxVolume].
- If the total cubes do not exceed the number of persons, then we check for an answer in the range [low, mid-1].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMaximumVloume( int a[], int n, int m)
{
int maxVolume = 0;
for ( int i = 0; i < n; i++) {
a[i] = a[i] * a[i] * a[i];
maxVolume = max(a[i], maxVolume);
}
int low = 0, high = maxVolume;
int maxVol = 0;
while (low <= high) {
int mid = (low + high) >> 1;
int cnt = 0;
for ( int i = 0; i < n; i++) {
cnt += a[i] / mid;
}
if (cnt >= m) {
low = mid + 1;
maxVol = max(maxVol, mid);
}
else
high = mid - 1;
}
return maxVol;
}
int main()
{
int a[] = { 1, 1, 1, 2, 2 };
int n = sizeof (a) / sizeof (a[0]);
int m = 3;
cout << getMaximumVloume(a, n, m);
return 0;
}
|
Java
class GFG
{
static int getMaximumVloume( int a[], int n, int m)
{
int maxVolume = 0 ;
for ( int i = 0 ; i < n; i++)
{
a[i] = a[i] * a[i] * a[i];
maxVolume = Math.max(a[i], maxVolume);
}
int low = 0 , high = maxVolume;
int maxVol = 0 ;
while (low <= high)
{
int mid = (low + high) >> 1 ;
int cnt = 0 ;
for ( int i = 0 ; i < n; i++)
{
cnt += a[i] / mid;
}
if (cnt >= m)
{
low = mid + 1 ;
maxVol = Math.max(maxVol, mid);
}
else
{
high = mid - 1 ;
}
}
return maxVol;
}
public static void main(String[] args)
{
int a[] = { 1 , 1 , 1 , 2 , 2 };
int n = a.length;
int m = 3 ;
System.out.println(getMaximumVloume(a, n, m));
}
}
|
Python3
def getMaximumVloume(a, n, m):
maxVolume = 0
for i in range (n):
a[i] = a[i] * a[i] * a[i]
maxVolume = max (a[i], maxVolume)
low = 0
high = maxVolume
maxVol = 0
while (low < = high):
mid = (low + high) >> 1
cnt = 0
for i in range (n):
cnt + = int (a[i] / mid)
if (cnt > = m):
low = mid + 1
maxVol = max (maxVol, mid)
else :
high = mid - 1
return maxVol
if __name__ = = '__main__' :
a = [ 1 , 1 , 1 , 2 , 2 ]
n = len (a)
m = 3
print (getMaximumVloume(a, n, m))
|
C#
using System;
class GFG
{
static int getMaximumVloume( int []a, int n, int m)
{
int maxVolume = 0;
for ( int i = 0; i < n; i++)
{
a[i] = a[i] * a[i] * a[i];
maxVolume = Math.Max(a[i], maxVolume);
}
int low = 0, high = maxVolume;
int maxVol = 0;
while (low <= high)
{
int mid = (low + high) >> 1;
int cnt = 0;
for ( int i = 0; i < n; i++)
{
cnt += a[i] / mid;
}
if (cnt >= m)
{
low = mid + 1;
maxVol = Math.Max(maxVol, mid);
}
else
{
high = mid - 1;
}
}
return maxVol;
}
public static void Main(String[] args)
{
int []a = {1, 1, 1, 2, 2};
int n = a.Length;
int m = 3;
Console.WriteLine(getMaximumVloume(a, n, m));
}
}
|
PHP
<?php
function getMaximumVloume( $a , $n , $m )
{
$maxVolume = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$a [ $i ] = $a [ $i ] * $a [ $i ] * $a [ $i ];
$maxVolume = max( $a [ $i ], $maxVolume );
}
$low = 0; $high = $maxVolume ;
$maxVol = 0;
while ( $low <= $high )
{
$mid = ( $low + $high ) >> 1;
$cnt = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$cnt += (int)( $a [ $i ] / $mid );
}
if ( $cnt >= $m )
{
$low = $mid + 1;
$maxVol = max( $maxVol , $mid );
}
else
$high = $mid - 1;
}
return $maxVol ;
}
$a = array (1, 1, 1, 2, 2);
$n = sizeof( $a );
$m = 3;
echo getMaximumVloume( $a , $n , $m );
?>
|
Javascript
<script>
function getMaximumVloume(a , n , m) {
var maxVolume = 0;
for (i = 0; i < n; i++) {
a[i] = a[i] * a[i] * a[i];
maxVolume = Math.max(a[i], maxVolume);
}
var low = 0, high = maxVolume;
var maxVol = 0;
while (low <= high) {
var mid = (low + high) >> 1;
var cnt = 0;
for (i = 0; i < n; i++) {
cnt += parseInt(a[i] / mid);
}
if (cnt >= m) {
low = mid + 1;
maxVol = Math.max(maxVol, mid);
}
else {
high = mid - 1;
}
}
return maxVol;
}
var a = [ 1, 1, 1, 2, 2 ];
var n = a.length;
var m = 3;
document.write(getMaximumVloume(a, n, m));
</script>
|
Time Complexity: O(N * log(maxVolume)), as we are using nested loops, the outer loop traverses log(maxVolume) times as in each traversal we do right shift by 1 bit which is equivalent to floor division of 2 therefore the effective time will be 1+1/2+1/4+…..+1/2^maxVolume which is equivalent to log(maxVolume), the inner loop traverses N times.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
29 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...