🔥Beats 100%🔥Easy & Visual Explaination | 18ms | Beginner Level | Binary Search Approach
:) Problem UnderstandingYou have an array candies, where each element represents a pile of candies. You need to distribute these candies among k children equall
# :) Problem Understanding

You have an array `candies`, where each element represents a pile of candies. You need to distribute these candies among `k` children equally, ensuring that each child gets the same number of candies. You can split the piles but cannot merge them together.

---

# :) Binary Search Solution (Efficient Approach)

We binary search on the possible values of `x` (candies per child), where `x` can be between `1` and `max(candies)`. 

### Step-by-Step Approach :

**1️⃣ Define the search range :**
- `left = 0` (minimum candies per child)
- `right = max(candies)` (maximum possible candies per child)

**2️⃣ Binary Search to find the largest possible x (candies per child) :**

- Midpoint (`mid`) represents a possible number of candies per child.
- Check feasibility: Can we distribute `mid` candies per child among `k` children?
- For each pile `y`, count how many children can get `y/mid` full shares.
- If the total number of children we can satisfy (`sum`) is greater than or equal to `k`, increase `mid` (search right).
- Otherwise, decrease `mid` (search left).

**3️⃣ Return the largest possible x that satisfies the condition.**

---

# :) Visual Intuition
Imagine you have `candies = [5,8,6]` and `k = 3`.

We binary search for `x` :

![temp5.png](https://assets.leetcode.com/users/images/277f5b63-ef9b-4e18-9da7-8109dc6529d9_1741934877.195846.png)


Thus, the answer is **5 candies per child**. 🎯

---
![upvote.jpg](https://assets.leetcode.com/users/images/0a03989a-c8ad-47a0-8a5c-d59dce67962d_1741935026.8360162.jpeg)

---

# :) Complexity
- Time complexity : O(N log M)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity : O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

---

# :) Bro'Code

```javascript []
var maximumCandies = function(candies, k) {
    let left =0,right = Math.max(...candies);

    const check = (x)=>{
        let sum =0;
        for(const y of candies){
            sum+=Math.floor(y/x);
        }
        return sum>=k;
    }

    while(left<=right){
        let mid = Math.floor((left+right)/2);
        if(!check(mid)){
            right = mid-1;
        }else{
            left = mid+1;
        }
    }
    return right;
};
```
```typescript []
function maximumCandies(candies: number[], k: number): number {
    let left = 0, right = Math.max(...candies);

    const canDistribute = (x: number): boolean => {
        let count = 0;
        for (const c of candies) {
            count += Math.floor(c / x);
        }
        return count >= k;
    };

    while (left <= right) {
        let mid = Math.floor((left + right) / 2);
        if (!canDistribute(mid)) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    
    return right;
}
```
```C []
bool canDistribute(int* candies, int candiesSize, long long k, int x) {
    long long count = 0;
    for (int i = 0; i < candiesSize; i++) {
        count += candies[i] / x;
    }
    return count >= k;
}

int maximumCandies(int* candies, int candiesSize, long long k) {
    int left = 0, right = 0;
    
    // Find the maximum value in the candies array
    for (int i = 0; i < candiesSize; i++) {
        if (candies[i] > right) {
            right = candies[i];
        }
    }

    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (!canDistribute(candies, candiesSize, k, mid)) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    
    return right;
}
```
```C++ []
class Solution {
public:
    bool canDistribute(vector<int>& candies, long long k, int x) {
        long long count = 0;
        for (int c : candies) {
            count += c / x;
        }
        return count >= k;
    }

    int maximumCandies(vector<int>& candies, long long k) {
        int left = 0, right = *max_element(candies.begin(), candies.end());

        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (!canDistribute(candies, k, mid)) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        
        return right;
    }
};
```
```C# []
public class Solution {
    public int MaximumCandies(int[] candies, long k) {
        int left = 0, right = candies.Max();

        bool CanDistribute(int x) {
            long count = 0;
            foreach (int c in candies) {
                count += c / x;
            }
            return count >= k;
        }

        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (!CanDistribute(mid)) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        
        return right;
    }
}
```
```java []
class Solution {
    public int maximumCandies(int[] candies, long k) {
        int left = 0, right = 0;

        // Find the maximum candy pile
        for (int candy : candies) {
            right = Math.max(right, candy);
        }

        // Binary search for the maximum candies per child
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (!canDistribute(candies, k, mid)) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }

        return right;
    }

    private boolean canDistribute(int[] candies, long k, int x) {
        if (x == 0) return false;
        
        long count = 0;
        for (int candy : candies) {
            count += candy / x;
        }
        
        return count >= k;
    }
}
```
```python []
class Solution(object):
    def maximumCandies(self, candies, k):
        """
        :type candies: List[int]
        :type k: int
        :rtype: int
        """
        left, right = 0, max(candies)

        def canDistribute(x):
            return sum(c // x for c in candies) >= k

        while left <= right:
            mid = (left + right) // 2
            if not canDistribute(mid):
                right = mid - 1
            else:
                left = mid + 1
        
        return right
```
```Go []
func maximumCandies(candies []int, k int64) int {
    left, right := 0, 0

    // Find the maximum value in candies array
    for _, c := range candies {
        if c > right {
            right = c
        }
    }

    // Binary search for the maximum candies per child
    for left <= right {
        mid := left + (right-left)/2
        if !canDistribute(candies, k, mid) {
            right = mid - 1
        } else {
            left = mid + 1
        }
    }
    
    return right
}

// Helper function to check if we can distribute 'x' candies per child
func canDistribute(candies []int, k int64, x int) bool {
    if x == 0 {
        return false
    }

    var count int64 = 0
    for _, c := range candies {
        count += int64(c / x)
    }
    
    return count >= k
}
```
```ruby []
def maximum_candies(candies, k)
    left, right = 0, candies.max

    # Helper function to check if we can distribute 'x' candies per child
    def can_distribute?(candies, k, x)
        candies.sum { |c| c / x } >= k
    end

    while left <= right
        mid = (left + right) / 2
        if !can_distribute?(candies, k, mid)
            right = mid - 1
        else
            left = mid + 1
        end
    end
    
    right
end
```

['Binary Search', 'C', 'Python', 'C++', 'Java', 'Go', 'TypeScript', 'Ruby', 'JavaScript', 'C#']