Binary Search.  Java | Kotlin | Python | Javascript. 100% runtime
IntuitionThe task is to find the number of candies that can be taken with the same amount among k childrenApproachThis problem can be solved with brute force so
# Intuition
The task is to find the number of candies that can be taken with the same amount among k children

# Approach
This problem can be solved with brute force solution easily by checking can k children take 1, 2, 3, 4 and so and until they cann't take it.
but this is not good in Time complexity which takes $$O(n^2)$$

**Optimal way using binary search**

- starting point 0
- end point the biggest item
- every time we check the middle number if it statisfy we continue checking on the right else we check the left.

# Complexity
- Time complexity:
O(n*Log(m)) m is the biggest item in the candies[i]

- Space complexity:
O(1)

# Code
```kotlin []
class Solution {
    fun maximumCandies(candies: IntArray, k: Long): Int {
       var sum = 0L
       var right  = 0
       for(item in candies){
        sum+=item
        right = max(right,item)
       } 
       if(sum<k)return 0
        var left = 0
        var result = 0
        while(left<=right){
            val mid = (left+right)/2
            if(helper(candies,mid,k)){
                result = mid
                left = mid+1
            }else{
                right = mid-1
            }
        }
        return result
    }
    fun helper(candies:IntArray,n:Int,k:Long):Boolean{
        var sum = 0L
        if(n==0)return true
        for(item in candies){
            sum+=(item/n)
        }
        return sum>=k
    }
}
```
```javascript []
class Solution {
    maximumCandies(candies, k) {
        let total = candies.reduce((sum, item) => sum + item, 0);
        let right = Math.max(...candies);
        if (total < k) return 0;
        
        let left = 0, result = 0;
        while (left <= right) {
            let mid = Math.floor((left + right) / 2);
            if (this.helper(candies, mid, k)) {
                result = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        
        return result;
    }

    helper(candies, n, k) {
        if (n === 0) return true;
        let total = candies.reduce((sum, item) => sum + Math.floor(item / n), 0);
        return total >= k;
    }
}

```
```python []
class Solution:
    def maximumCandies(self, candies: list[int], k: int) -> int:
        total = sum(candies)
        right = max(candies)
        if total < k:
            return 0
        
        left, result = 0, 0
        while left <= right:
            mid = (left + right) // 2
            if self.helper(candies, mid, k):
                result = mid
                left = mid + 1
            else:
                right = mid - 1
        
        return result

    def helper(self, candies: list[int], n: int, k: int) -> bool:
        if n == 0:
            return True
        total = sum(item // n for item in candies)
        return total >= k
```
```Java []
import java.util.*;

class Solution {
    public int maximumCandies(int[] candies, long k) {
        long total = Arrays.stream(candies).sum();
        int right = Arrays.stream(candies).max().getAsInt();
        if (total < k) return 0;
        
        int left = 0, result = 0;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (helper(candies, mid, k)) {
                result = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        
        return result;
    }

    private boolean helper(int[] candies, int n, long k) {
        if (n == 0) return true;
        long total = 0;
        for (int item : candies) {
            total += item / n;
        }
        return total >= k;
    }
}

```

['Python', 'Java', 'Kotlin', 'JavaScript']