Assign Maximum Cookies (original) (raw)

Last Updated : 2 Sep, 2025

Given two arrays, **greed[] and **cookie[] such that greed[i]denotes the minimum cookie size wanted by ith child and cookie[i] denotes the size of ith cookie, we have to find the maximum number of children that can be satisfied by assigning them cookies, with each child getting at most 1 cookie.

**Note: A child will be satisfied if he is assigned a cookie of size at least equal to his greed. In other words, the ith child will be satified with jth cookie only if greed[i] <= cookie[j].

**Examples:

**Input: greed[] = [1, 10, 3], cookie[] = [1, 2,3]
**Output: 2
**Explanation: We can only assign cookie to the first child and third child.

**Input: greed[] = [10,100], cookie[] = [1, 2]
**Output: 0
**Explanation: We can not assign cookies to children.

Try It Yourselfredirect icon

[Approach] Using Sorting and Two Pointer - O(nlogn + mlogm) Time and O(1) Space

The idea is to assign smallest cookie to each child greedily such that the child gets satisfied. So, we can sort both **greed[] and **cookie[] and start assigning the smallest cookies to the children with smallest greed.

**Steps to solve the problem:

#include #include #include using namespace std;

int maxChildren(vector &greed, vector &cookie) { sort(greed.begin(), greed.end()); sort(cookie.begin(), cookie.end());

// Initialize pointers to the beginning of the arrays
int i = 0, j = 0;
int cnt = 0;

// Iterate until we reach the end of either array
while(i < greed.size() && j < cookie.size()) {
  
    // If ith child can be satisfied with jth cookie,
    // assign it to him and increment the count 
    if(greed[i] <= cookie[j]) {
        cnt++;
        i++;
        j++;
    }
    
    // If ithe child cannot be satified with jth 
    // cookie, move to the next bigger cookie
    else 
        j++;
}
return cnt;

}

int main() { vector greed = {1, 10, 3}; vector cookie = {1, 2,3};

cout << maxChildren(greed, cookie);

}

C

// C Code for assigning cookies using Greedy Algorithm

#include <stdio.h>

int compare(const void *a, const void b) { return ((int *)a - *(int *)b); }

int maxChildren(int greed[], int n, int cookie[], int m) { qsort(greed, n, sizeof(int), compare); qsort(cookie, m, sizeof(int), compare);

// Initialize pointers to the beginning of the arrays
int i = 0, j = 0;
int cnt = 0;

// Iterate until we reach the end of either array
while (i < n && j < m) {
    
    // If ith child can be satisfied with jth cookie,
    // assign it to him and increment the count 
    if (greed[i] <= cookie[j]) {
        cnt++;
        i++;
        j++;
    }
    
    // If ith child cannot be satisfied with jth 
    // cookie, move to the next bigger cookie
    else 
        j++;
}
return cnt;

}

int main() { int greed[] = {1, 10, 3}; int cookie[] = {1, 2,3}; int n = sizeof(greed)/sizeof(greed[0]); int m = sizeof(cookie)/sizeof(cookie[0]); int result = maxChildren(greed, n, cookie, m);

printf("%d\n", result);
return 0;

}

Java

import java.util.Arrays;

class GfG { static int maxChildren(int[] greed, int[] cookie) { Arrays.sort(greed); Arrays.sort(cookie);

    // Initialize pointers to the beginning of the arrays
    int i = 0, j = 0;
    int cnt = 0;
  
    // Iterate until we reach the end of either array
    while (i < greed.length && j < cookie.length) {
      
        // If ith child can be satisfied with jth cookie,
        // assign it to him and increment the count 
        if (greed[i] <= cookie[j]) {
            cnt++;
            i++;
            j++;
        }
        
        // If ith child cannot be satisfied with jth 
        // cookie, move to the next bigger cookie
        else 
            j++;
    }
    return cnt;
}

public static void main(String[] args) {
    int[] greed = {1, 10, 3};
    int[] cookie = {1, 2,3};
  
    System.out.println(maxChildren(greed, cookie));
}

}

Python

def maxChildren(greed, cookie): greed.sort() cookie.sort()

# Initialize pointers to the beginning of the arrays
i = 0
j = 0
cnt = 0

# Iterate until we reach the end of either array
while i < len(greed) and j < len(cookie):
  
    # If ith child can be satisfied with jth cookie,
    # assign it to him and increment the count 
    if greed[i] <= cookie[j]:
        cnt += 1
        i += 1
        j += 1
    
    # If ith child cannot be satisfied with jth 
    # cookie, move to the next bigger cookie
    else:
        j += 1
        
return cnt

if name == "main": greed = [1, 10, 3] cookie = [1, 2,3]

print(maxChildren(greed, cookie))

C#

using System;

class GfG { static int MaxChildren(int[] greed, int[] cookie) { Array.Sort(greed); Array.Sort(cookie);

    // Initialize pointers to the beginning of the arrays
    int i = 0, j = 0;
    int cnt = 0;
  
    // Iterate until we reach the end of either array
    while (i < greed.Length && j < cookie.Length) {
      
        // If ith child can be satisfied with jth cookie,
        // assign it to him and increment the count 
        if (greed[i] <= cookie[j]) {
            cnt++;
            i++;
            j++;
        }
        
        // If ith child cannot be satisfied with jth 
        // cookie, move to the next bigger cookie
        else
            j++;
    }
    return cnt;
}

static void Main(string[] args) {
    int[] greed = { 1, 10, 3 };
    int[] cookie = { 1, 2,3 };
  
    Console.WriteLine(MaxChildren(greed, cookie));
}

}

JavaScript

function maxChildren(greed, cookie) { greed.sort((a, b) => a - b); cookie.sort((a, b) => a - b);

// Initialize pointers to the beginning of the arrays
let i = 0, j = 0;
let cnt = 0;

// Iterate until we reach the end of either array
while (i < greed.length && j < cookie.length) {
  
    // If ith child can be satisfied with jth cookie,
    // assign it to him and increment the count 
    if (greed[i] <= cookie[j]) {
        cnt++;
        i++;
        j++;
    }
    
    // If ith child cannot be satisfied with jth 
    // cookie, move to the next bigger cookie
    else 
        j++;
}
return cnt;

}

// Driver Code const greed = [1, 10, 3]; const cookie = [1, 2,3]; console.log(maxChildren(greed, cookie));

`