Simple and Beginner-Friendly Approach to Maximum Candies Problem
IntuitionThe problem requires us to distribute candies among k children such that each child gets the maximum possible equal number of candies. Since distributi

---

## **Intuition**
The problem requires us to distribute candies among `k` children such that each child gets the maximum possible equal number of candies. Since distributing `x` candies per child means checking whether we can form at least `k` groups, we need to find the **maximum possible `x`** that satisfies this condition.

A naive approach would be to iterate from 1 to the maximum number of candies and check if it's possible to distribute them, but this is inefficient. Instead, we can use **binary search** to efficiently find the maximum value of `x`.

---

## **Approach**
1. **Determine the Search Space:**  
   - The minimum possible candies a child can get is `1`.
   - The maximum is `max(candies)`, as we cannot give more than what we have in the largest pile.

2. **Binary Search on the Answer:**  
   - Use binary search between `low = 1` and `high = max(candies)`.
   - Calculate `mid = (low + high) / 2`, which represents a possible number of candies each child can get.
   - Check if it's possible to distribute at least `k` groups of `mid` candies using the `valid()` function:
     - Iterate through `candies`, summing up `candies[i] / mid` (i.e., how many full groups of `mid` can be formed).
     - If the total count is `>= k`, it's a valid distribution.
   - If `valid(mid) == true`, update `ans = mid` and move `low` to `mid + 1` (search for a larger possible value).
   - Otherwise, move `high` to `mid - 1` (reduce the number of candies per child).

3. **Return `ans` as the maximum valid `mid`.**

---

## **Complexity Analysis**
- **Time Complexity:**  
  - We perform a binary search on values from `1` to `max(candies)`, which takes **\( O( log M) \)** iterations, where \( M \) is the maximum number of candies in any pile.
  - Each binary search step requires checking all elements in `candies`, which is **\( O(N) \)**.
  - **Total time complexity:** \( O(N log M) \).

- **Space Complexity:**  
  - We use only a few extra variables (`low`, `high`, `mid`, etc.), so the space complexity is **\( O(1) \)**.

---
# Code
```java []
class Solution {
    public int maximumCandies(int[] candies, long k) {
        int max=candies[0];
        for(int i=1;i<candies.length;i++)
        {
            max=Math.max(candies[i],max);
        }
        int low=1,high=max;
        int ans=0;
        while(low<=high)
        {
           int mid=(low+high)/2;
           if(valid(mid,candies,k))
           {
            ans=mid;
            low=mid+1;
           }
           else
           {
            high=mid-1;
           }
        }
        return ans;
    }
    public  boolean valid(int mid,int candies[],long k)
    {
        long c=0;
        for(int i=0;i<candies.length;i++)
        {
          if(candies[i]>=mid)
          {
            c+=candies[i]/mid;
          }
        }
        if(c>=k)
        return true;
        return false;
    }
}
```
---
['Array', 'Binary Search', 'Java']