Open In App

Assign Cookies

Last Updated : 26 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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, the task is 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, 2, 3], cookie[] = [1, 1]
Output: 1
Explanation: We can only assign the first or second cookie to the first child.

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

Using Greedy Algorithm

The idea is to use Greedy Algorithm to assign smallest cookie to each child such that the child gets satisfied. So, we can sort both greed[] and cookie[] array and start assigning the smallest cookies to the children with smallest greed. We can initialize two pointers, say i and j to the beginning of greed[] and cookie[] respectively. Now, we can have 2 cases:

  • If greed[i] <= cookie[j], this means that we can satisfy the ith child by assigning him jth cookie. So, we increase the count of satisfied children by 1 and increment both the pointers to move to the next child and cookie.
  • If greed[i] > cookie[j], this means that we cannot satisfy the ith child by assigning him jth cookie as he wants a bigger cookie. So, increment j to move to the next bigger cookie.

We continue traversing the array until either pointer reaches the end of its respective array.

C++
// C++ Code for assigning cookies using Greedy Algorithm

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int maxChildren(vector<int> &greed, vector<int> &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<int> greed = {1, 2, 3};
    vector<int> cookie = {1, 1};
  
    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, 2, 3};
    int cookie[] = {1, 1};
    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
// Java Code for assigning cookies using Greedy Algorithm

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, 2, 3};
        int[] cookie = {1, 1};
      
        System.out.println(maxChildren(greed, cookie));
    }
}
Python
# Python Code for assigning cookies using Greedy Algorithm

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, 2, 3]
    cookie = [1, 1]
  
    print(maxChildren(greed, cookie))
C#
// C# Code for assigning cookies using Greedy Algorithm

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, 2, 3 };
        int[] cookie = { 1, 1 };
      
        Console.WriteLine(MaxChildren(greed, cookie));
    }
}
JavaScript
// JavaScript Code for assigning cookies using Greedy Algorithm

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, 2, 3];
const cookie = [1, 1];
console.log(maxChildren(greed, cookie));

Output
1

Time Complexity: O(nlogn + mlogm), where n and m are the sizes of greed[] and cookie[] respectively. This is because we are sorting both the arrays before traversing over them.
Auxiliary Space: O(1)


Article Tags :
Practice Tags :

Similar Reads