Maximum Candies Allocated to K Children

Problem

//same as coco eating bananas,
//capacity to ship packages within d days
//aggresive cows
class Solution {
public int maximumCandies(int[] candies, long k) {
long sum = 0;
int max = 0;
for(int i : candies){


This content originally appeared on DEV Community and was authored by Prashant Mishra

Problem

//same as coco eating bananas, 
//capacity to ship packages within d days
//aggresive cows
class Solution {
    public int maximumCandies(int[] candies, long k) {
        long sum = 0;
        int max = 0;
        for(int i : candies){
            sum+=i;
            max = Math.max(max, i);//max no. of candy that can be allocated to child from the same lot
        }
        if(sum<k) return 0;

        int low =1;// lowest no. of candy that can be allocated to child from the same lot
        int high = max;

        while(low<=high){
            int mid = (low+high)/2;
            if(is(candies,k,mid)){
                low = mid+1;
            }
            else high = mid-1;
        }
        return high;
    }
    public boolean is(int arr[], long k,int target){
        long children = 0;
        for(int i : arr){
            if(i >= target){ // if the candy lot size is greater that current target(max candy allocation value from the same lot)
                children+=i/target;// then we can get assign i/target no. of children the same no. of candies
            }
        }
        return children>=k;// if the no. of children is greater than the k (childrens) then target is one of the valid candy lot size
    }
}


This content originally appeared on DEV Community and was authored by Prashant Mishra


Print Share Comment Cite Upload Translate Updates
APA

Prashant Mishra | Sciencx (2025-03-14T09:22:28+00:00) Maximum Candies Allocated to K Children. Retrieved from https://www.scien.cx/2025/03/14/maximum-candies-allocated-to-k-children/

MLA
" » Maximum Candies Allocated to K Children." Prashant Mishra | Sciencx - Friday March 14, 2025, https://www.scien.cx/2025/03/14/maximum-candies-allocated-to-k-children/
HARVARD
Prashant Mishra | Sciencx Friday March 14, 2025 » Maximum Candies Allocated to K Children., viewed ,<https://www.scien.cx/2025/03/14/maximum-candies-allocated-to-k-children/>
VANCOUVER
Prashant Mishra | Sciencx - » Maximum Candies Allocated to K Children. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/14/maximum-candies-allocated-to-k-children/
CHICAGO
" » Maximum Candies Allocated to K Children." Prashant Mishra | Sciencx - Accessed . https://www.scien.cx/2025/03/14/maximum-candies-allocated-to-k-children/
IEEE
" » Maximum Candies Allocated to K Children." Prashant Mishra | Sciencx [Online]. Available: https://www.scien.cx/2025/03/14/maximum-candies-allocated-to-k-children/. [Accessed: ]
rf:citation
» Maximum Candies Allocated to K Children | Prashant Mishra | Sciencx | https://www.scien.cx/2025/03/14/maximum-candies-allocated-to-k-children/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.