Distribute Candies

Alice has n candies, where the ith candy is of type candyType[i]. A doctor told her to eat only n / 2 of them (n is always even). She wants to eat as many different types of candy as possible. Given candyType, return the maximum number of different types she can eat if she eats only n / 2 of them.

30:00

Distribute Candies
easy
Free written solution
Topics
Companies

Alice has n candies, where the ith candy is of type candyType[i]. A doctor told her to eat only n / 2 of them (n is always even). She wants to eat as many different types of candy as possible. Given candyType, return the maximum number of different types she can eat if she eats only n / 2 of them.

Example 1:
Input: {"candyType":[1,1,2,2,3,3]}
Output: 3
Constraints:
  • n==candyType.lengthn == \text{candyType.length}

  • 2≤n≤1042 \leq n \leq 10^4

  • nn is even.

  • −105≤candyType[i]≤105-10^5 \leq \text{candyType}[i] \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"candyType":[1,1,2,2,3,3]}

Alice may eat n / 2 = 3 of the 6 candies. Her plate has 3 seats, and she wants as many different types on it as possible.

the candy bag (6 candies)

1
1
2
2
3
3
✓ new type, takes a seat✕ repeat, adds no variety

Alice's plate: n / 2 = 3 seats

0 types / 3 seats

answer = min(types, n / 2) = min(0, 3)

Variables
VariableValue
n6
limit3
kinds0
DepthFunction Call
Stack empty
0/7