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 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:

  

#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;

}

Time Complexity: O(N * log (maxVolume))



Striver(underscore)79 at Codechef and codeforces D


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.

Article Tags :



Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.








Source link

Ceiling in right side for every element in an array


Given an array of integers, find the closest greater element for every element. If there is no greater element then print -1

Examples:

Input : arr[] = {10, 5, 11, 10, 20, 12}
Output : 10 10 12 12 -1 -1

Input : arr[] = {50, 20, 200, 100, 30}
Output : 100 30 -1 -1 -1

A simple solution is to run two nested loops. We pick an outer element one by one. For every picked element, we traverse right side array and find closest greater or equal element. Time complexity of this solution is O(n*n)

A better solution is to use sorting. We sort all elements, then for every element, traverse toward right until we find a greater element (Note that there can be multiple occurrences of an element).

An efficient solution is to use Self Balancing BST (Implemented as set in C++ and TreeSet in Java). In a Self Balancing BST, we can do both insert and ceiling operations in O(Log n) time.

import java.util.*;

  

class TreeSetDemo {

    public static void closestGreater(int[] arr)

    {

        int n = arr.length;

        TreeSet<Integer> ts = new TreeSet<Integer>();

        ArrayList<Integer> ceilings = new ArrayList<Integer>(n);

  

        

        

        for (int i = n - 1; i >= 0; i--) {

            Integer greater = ts.ceiling(arr[i]);

            if (greater == null)

                ceilings.add(-1);

            else

                ceilings.add(greater);

            ts.add(arr[i]);

        }

  

        for (int i=n-1; i>=0; i--)

           System.out.print(ceilings.get(i) + " ");

    }

  

    public static void main(String[] args)

    {

        int[] arr = {50, 20, 200, 100, 30};

        closestGreater(arr);

    }

}

Time Complexity : O(n Log n)




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.

Article Tags :



Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.








Source link