FastPrepAssign Cookies With Matching Parity
Problem · Array

Assign Cookies With Matching Parity

Learn this problem
Mediuminfosys logoinfosysNEW GRADONSITE INTERVIEW

Problem statement

You are given two integer arrays g and s.

g[i] is the greed factor of child i. s[j] is the size of cookie j.

Assign at most one cookie to each child. Cookie j may be assigned to child i only when s[j] >= g[i] and s[j] and g[i] have the same parity: both even or both odd.

Return the maximum number of children who can be assigned a cookie.

Function

findContentChildren(g: int[], s: int[]) → int

Examples

Example 1

g = [1,2,3]s = [1,2,3]return = 3

Each child can take the cookie of equal size. The pairs 1, 2, and 3 all have matching parity.

Example 2

g = [1,3]s = [2,4]return = 0

Both children have odd greed, and both cookies are even, so no assignment is legal.

Constraints

  • 1 <= g.length <= 3 * 10^4.
  • 0 <= s.length <= 3 * 10^4.
  • 1 <= g[i], s[j] <= 2^31 - 1.

More infosys problems

drafts saved locally
public int findContentChildren(int[] g, int[] s) {
  // Write your code here.
}
g[1,2,3]
s[1,2,3]
expected3
checking account