Given an array of N integers which denotes the edges of N cubical structures respectively. Also given are M integers which denotes 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 of any shape from any of the N cubes.
Examples:
Input: a[] = {1, 1, 1, 2, 2}, m = 3
Output: 4
All three person 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 of 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 volumes of all of 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 of 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++
// C++ program to implement the above approach #include <bits/stdc++.h> using namespace std; // Function to get the maximum volume that // every person can get int getMaximumVloume(int a[], int n, int m) { int maxVolume = 0; // Convert the length to respective volumes // and find the maximum volumes for (int i = 0; i < n; i++) { a[i] = a[i] * a[i] * a[i]; maxVolume = max(a[i], maxVolume); } // Apply binary search with intiial // low as 0 and high as the maximum most int low = 0, high = maxVolume; // Initial answer is 0 slices int maxVol = 0; // Apply binary search while (low <= high) { // Get the mid element int mid = (low + high) >> 1; // Count the slices of volume mid int cnt = 0; for (int i = 0; i < n; i++) { cnt += a[i] / mid; } // If the slices of volume // exceeds the number of persons // then every person can get volume mid if (cnt >= m) { // Then check for larger in the right half low = mid + 1; // Replace tbe answer with // current maximum i.e., mid maxVol = max(maxVol, mid); } // else traverse in the left half else high = mid - 1; } return maxVol; } // Driver code 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
// Java program to implement the above approach class GFG { // Function to get the maximum volume that // every person can get static int getMaximumVloume(int a[], int n, int m) { int maxVolume = 0; // Convert the length to respective volumes // and find the maximum volumes for (int i = 0; i < n; i++) { a[i] = a[i] * a[i] * a[i]; maxVolume = Math.max(a[i], maxVolume); } // Apply binary search with intiial // low as 0 and high as the maximum most int low = 0, high = maxVolume; // Initial answer is 0 slices int maxVol = 0; // Apply binary search while (low <= high) { // Get the mid element int mid = (low + high) >> 1; // Count the slices of volume mid int cnt = 0; for (int i = 0; i < n; i++) { cnt += a[i] / mid; } // If the slices of volume // exceeds the number of persons // then every person can get volume mid if (cnt >= m) { // Then check for larger in the right half low = mid + 1; // Replace tbe answer with // current maximum i.e., mid maxVol = Math.max(maxVol, mid); } // else traverse in the left half else { high = mid - 1; } } return maxVol; } // Driver code 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)); } } // This code is contributed by 29AjayKumar |
Python3
# Python 3 program to implement # the above approach # Function to get the maximum volume # that every person can get def getMaximumVloume(a, n, m): maxVolume = 0 # Convert the length to respective # volumes and find the maximum volumes for i in range(n): a[i] = a[i] * a[i] * a[i] maxVolume = max(a[i], maxVolume) # Apply binary search with intiial # low as 0 and high as the maximum most low = 0 high = maxVolume # Initial answer is 0 slices maxVol = 0 # Apply binary search while (low <= high): # Get the mid element mid = (low + high) >> 1 # Count the slices of volume mid cnt = 0 for i in range(n): cnt += int(a[i] / mid) # If the slices of volume # exceeds the number of persons # then every person can get volume mid if (cnt >= m): # Then check for larger in the right half low = mid + 1 # Replace tbe answer with # current maximum i.e., mid maxVol = max(maxVol, mid) # else traverse in the left half else: high = mid - 1 return maxVol # Driver code if __name__ == '__main__': a = [1, 1, 1, 2, 2] n = len(a) m = 3 print(getMaximumVloume(a, n, m)) # This code is contributed # by Surendra_Gangwar |
C#
// C# program to implement the above approach using System; class GFG { // Function to get the maximum volume that // every person can get static int getMaximumVloume(int []a, int n, int m) { int maxVolume = 0; // Convert the length to respective volumes // and find the maximum volumes for (int i = 0; i < n; i++) { a[i] = a[i] * a[i] * a[i]; maxVolume = Math.Max(a[i], maxVolume); } // Apply binary search with intiial // low as 0 and high as the maximum most int low = 0, high = maxVolume; // Initial answer is 0 slices int maxVol = 0; // Apply binary search while (low <= high) { // Get the mid element int mid = (low + high) >> 1; // Count the slices of volume mid int cnt = 0; for (int i = 0; i < n; i++) { cnt += a[i] / mid; } // If the slices of volume // exceeds the number of persons // then every person can get volume mid if (cnt >= m) { // Then check for larger in the right half low = mid + 1; // Replace tbe answer with // current maximum i.e., mid maxVol = Math.Max(maxVol, mid); } // else traverse in the left half else { high = mid - 1; } } return maxVol; } // Driver code 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)); } } /* This code contributed by PrinciRaj1992 */ |
PHP
<?php // PHP program to implement the above approach // Function to get the maximum volume that // every person can get function getMaximumVloume($a, $n, $m) { $maxVolume = 0; // Convert the length to respective volumes // and find the maximum volumes for ($i = 0; $i < $n; $i++) { $a[$i] = $a[$i] * $a[$i] * $a[$i]; $maxVolume = max($a[$i], $maxVolume); } // Apply binary search with intiial // low as 0 and high as the maximum most $low = 0; $high = $maxVolume; // Initial answer is 0 slices $maxVol = 0; // Apply binary search while ($low <= $high) { // Get the mid element $mid = ($low + $high) >> 1; // Count the slices of volume mid $cnt = 0; for ($i = 0; $i < $n; $i++) { $cnt += (int)($a[$i] / $mid); } // If the slices of volume // exceeds the number of persons // then every person can get volume mid if ($cnt >= $m) { // Then check for larger in the right half $low = $mid + 1; // Replace tbe answer with // current maximum i.e., mid $maxVol = max($maxVol, $mid); } // else traverse in the left half else $high = $mid - 1; } return $maxVol; } // Driver code $a = array(1, 1, 1, 2, 2); $n = sizeof($a); $m = 3; echo getMaximumVloume($a, $n, $m); // This code is contributed by Akanksha Rai ?> |
4
Time Complexity: O(N * log (maxVolume))
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.
Recommended Posts:
- Check if X can give change to every person in the Queue
- Maximum length of rod for Q-th person
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Sum of cubes of all Subsets of given Array
- Find the distance between two person after reconstruction of queue
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Sum of all Perfect Cubes lying in the range [L, R] for Q queries
- Sort the array according to their cubes of each element
- Largest number in an array that is not a perfect cube
- Largest perfect cube number in an Array
- Smallest perfect Cube divisible by all elements of an array
- Smallest perfect cube in an array
- Count of pairs in an Array whose sum is a Perfect Cube
- Construct an Array of size N whose sum of cube of all elements is a perfect square
- Construct an Array such that cube sum of all element is a perfect square
- C Program to check whether a number is a Perfect Cube or not
- Count of subarrays having sum as a perfect cube
- Maximum possible XOR of every element in an array with another array
- Maximum Unique Element in every subarray of size K
- Maximums from array when the maximum decrements after every access
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

