Distribute Candies

EasyArrays & Hashinghashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Alice has n candies, where n is even, and candyType[i] is the type of the i-th candy. Her doctor says she may eat only n / 2 of them. Alice wants to taste as many different types as possible while eating exactly n / 2 candies. Return the maximum number of distinct candy types she can eat.

Examples

Input: candyType = [1,1,2,2,3,3]
Output: 3
She can eat 3 candies and there are 3 types, so she picks one of each.
Input: candyType = [1,1,2,3]
Output: 2
She may eat 2 candies; picking types 1 and 2 (or any two distinct types) gives 2.
Input: candyType = [6,6,6,6]
Output: 1
Every candy is the same type.

Constraints

  • n == candyType.length
  • 2 <= n <= 10^4
  • `n` is even
  • -10^5 <= candyType[i] <= 10^5

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems