dsa · easy
Distribute Candies
candy_type has even length n. Each entry is the type of one candy. You will give exactly n / 2 candies to your sibling (any subset of that size). Keep the other half. Return the maximum number of **different** types you can keep for yourself.
Example
candy_type = [1, 1, 1, 2, 2, 3]. n = 6, so you keep 3 candies. The three types {1, 2, 3} are all present, and 3 is exactly the size of your half, so you can keep one of each → 3.
candy_type = [1, 1, 1, 1]. n = 4, you keep 2, but there is only one type in the whole bag → 1.
candy_type = [5, 6]. You keep 1 candy, and two types exist, so the cap is the half-size → 1.
## Arguments - candy_type — even-length array; candy_type[i] is the type of candy i
Constraints
2 <= candy_type.length <= 4*10^4 candy_type.length is even -10^5 <= candy_type[i] <= 10^5 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [1,1,1,2,2,3] Expected: 3
Example 2
Input: [1,1,1,1] Expected: 1
Example 3
Input: [5,6] Expected: 1