Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Assign Cookies

Easy
0/40
Average time to solve is 13m
profile
Contributed by
15 upvotes

Problem statement

There are ‘N’ children and ‘M’ cookies.




You must return the maximum number of children whose greed can be satisfied.


Example:
Input:
GREED = [3, 5, 6, 4], SIZE = [1, 4, 5], N = 4, M = 3

Output:
2

Explanation: We can assign the second cookie to the fourth child and the third to the second.
Hence we return 2.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input will contain two integers, ‘N’ and ‘M’, denoting the length of the array ‘GREED’ and ‘SIZE’.

The second line contains ‘N’ space-separated integers of the array ‘GREED’.

The third line contains ‘M’ space-separated integers of the array ‘SIZE’.
Output Format:-
The only line consists of a single integer denoting the maximum number of children whose greed can be satisfied. 
Note:-
You don’t need to print anything. Just implement the given function.
Sample Input 1:
3 4
1 2 3 
1 2 3 4
Sample Output 1:
3
Explanation Of Sample Input 1:
Input:
GREED = [1, 2, 3], SIZE = [1, 2, 3, 4], N = 3, M = 4

Output:
3

Explanation: We can assign the first cookie to the first child, the second cookie to the second child, and the third cookie to the third child.
Hence we return 2.
Sample Input 2:
5 5
4 5 6 7 8
2 3 4 5 6
Sample Output 2:
3
Constraints:
1 <= N, M <= 10^5
1 <= GREED[i], SIZE[j] <= 10^9
Time Limit: 1 sec
Full screen
Console