C# Solution for Maximum Candies Allocated To K Children Problem
IntuitionWe need to distribute candies among k children such that:
Each child gets the same number of candies.
A child can receive candies only from one pile.
S
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We need to distribute candies among k children such that:
1.	Each child gets the same number of candies.
2.	A child can receive candies only from one pile.
3.	Some piles may go unused.
4.	The goal is to maximize the number of candies each child gets.

Since we can divide but not merge piles, we need to determine the largest possible number of candies per child that allows at least k children to receive candies.

This naturally suggests a binary search approach

# Approach
<!-- Describe your approach to solving the problem. -->
1.	Define the Search Space:
	•	Minimum candies per child = 1 (if distribution is possible).
	•	Maximum candies per child = max(candies), since no child can get more than the largest pile.
2.	Binary Search Execution:
	•	Pick mid = (left + right) / 2 as the potential candies per child.
	•	Count how many children can be served if each gets exactly mid candies.
	•	If we can serve at least k children, it means we might be able to give more, so we search in the upper half (left = mid + 1).
	•	Otherwise, we reduce mid and search in the lower half (right = mid - 1).
3.	Checking Feasibility:
	•	Given mid candies per child, we iterate through candies[] and compute: total children served = sum frac{candies[i]}{mid}
	•	If total children served >= k, mid is a valid candidate.
4.	Return the best valid mid found.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
•	Binary Search: The search space is from 1 to max(candies), which means O(log max(candies)) ≈ O(24).
•	Feasibility Check: For each mid, we iterate through candies, which takes O(n).
•	Total Complexity: O(n log max(candies)) = O(10^5 x 24) = O(2.4 x 10^6)
This is efficient.

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
•	Binary search variables (left, right, mid, result): O(1)
•	Feasibility check (count): O(1)
•	Total Space Complexity: O(1) (constant extra space used).

# Code
```csharp []
public class Solution {
    public int MaximumCandies(int[] candies, long k) {
        if (candies.Sum(c => (long)c) < k) return 0; 
        
        int left = 1, right = candies.Max();
        int result = 0;

        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (CanDistribute(candies, k, mid)) {
                result = mid; 
                left = mid + 1; 
            } else {
                right = mid - 1; 
            }
        }

        return result;
    }
    private bool CanDistribute(int[] candies, long k, int mid) {
        long count = 0;
        foreach (int candy in candies) {
            count += candy / mid; 
            if (count >= k) return true; 
        }
        return count >= k;
    }
}
```
['C#']