---
# System prepended metadata

title: 2305. Fair Distribution of Cookies
tags: [Leetcode, Medium, Backtracking]

---

# 2305. Fair Distribution of Cookies
###### tags: `Leetcode` `Medium` `Backtracking`
Link: https://leetcode.com/problems/fair-distribution-of-cookies/description/
## Code
```java=
class Solution {
    int ans = Integer.MAX_VALUE;
    public int distributeCookies(int[] cookies, int k) {
        int[] children = new int[k];
        backtracking(cookies, 0, children, k);
        return ans;
    }
    public void backtracking(int[] cookies, int start, int[] children, int k){
        if(start==cookies.length){
            int max = 0;
            for(int i=0; i<k; i++){
                max = Math.max(max, children[i]);
            }
            ans = Math.min(ans, max);
            return;
        }
        for(int i=0; i<k; i++){
            children[i] += cookies[start];
            backtracking(cookies, start+1, children, k);
            children[i] -= cookies[start];
        }
    }
}
```