✅Range Based Binary Search 🔥| Visualization ✨| Math | Python | C++ | Java ✨
Approach
Complexity

Time complexity:   O(nlog(m))


Space complexity:  O(1)

Code
# Approach
![image.png](https://assets.leetcode.com/users/images/84844262-afdf-4d58-9e71-a35c5b36e1f9_1741918490.6617038.png)
![image.png](https://assets.leetcode.com/users/images/23254200-8e34-495b-9652-79e6318bcede_1741918627.5679643.png)



# Complexity
- Time complexity:   $$O(nlog(m))$$

- Space complexity:  $$O(1)$$ 

# Code
```python []
class Solution(object):
    def maximumCandies(self, candies, k):
        def can_allocate(candies, k, pile_size):
            if pile_size == 0:
                return True
            total_piles = 0
            for candy in candies:
                total_piles += candy // pile_size
                if total_piles >= k:
                    return True
            return False

        if sum(candies) < k:
            return 0

        low, high = 1, max(candies)
        while low < high:
            mid = (low + high + 1) // 2
            if can_allocate(candies, k, mid):
                low = mid
            else:
                high = mid - 1

        return low

```
```C++ []
class Solution {
public:
    int maximumCandies(vector<int>& candies, long long k) {
        auto canAllocate = [&](vector<int>& candies, long long k, int pileSize) {
            if (pileSize == 0) return true;
            long long totalPiles = 0;
            for (int candy : candies) {
                totalPiles += candy / pileSize;
                if (totalPiles >= k) return true;
            }
            return false;
        };
        
        long long sum = 0;
        for (int candy : candies) sum += candy;
        if (sum < k) return 0;

        int low = 1, high = *max_element(candies.begin(), candies.end());
        while (low < high) {
            int mid = (low + high + 1) / 2;
            if (canAllocate(candies, k, mid)) {
                low = mid;
            } else {
                high = mid - 1;
            }
        }
        return low;
    }
};


```
```Java []
class Solution {
    public int maximumCandies(int[] candies, long k) {
        if (sum(candies) < k) {
            return 0;
        }

        int low = 1, high = getMax(candies);
        while (low < high) {
            int mid = (low + high + 1) / 2;
            if (canAllocate(candies, k, mid)) {
                low = mid;
            } else {
                high = mid - 1;
            }
        }
        return low;
    }

    private boolean canAllocate(int[] candies, long k, int pileSize) {
        if (pileSize == 0) return true;
        long totalPiles = 0;
        for (int candy : candies) {
            totalPiles += candy / pileSize;
            if (totalPiles >= k) return true;
        }
        return false;
    }

    private long sum(int[] candies) {
        long sum = 0;
        for (int candy : candies) {
            sum += candy;
        }
        return sum;
    }

    private int getMax(int[] candies) {
        int max = 0;
        for (int candy : candies) {
            max = Math.max(max, candy);
        }
        return max;
    }
}


```
![image.png](https://assets.leetcode.com/users/images/1a1a6be1-8d0b-4c28-8cf6-f1d489aee2a5_1741918691.0956779.png)

['Array', 'Math', 'Binary Search', 'Python', 'C++', 'Java']