Ctrl K

Half a Box, Most Flavors

Easy
You got a box of n sweets, where n is even. The flavor of the i-th sweet is the number sweets[i]; equal numbers mean the same flavor. You decide to eat exactly half the box, n / 2 sweets, and want to try as many different flavors as possible. Return the largest number of distinct flavors you can get.
Example 1
sweets=[4, 4, 4, 7, 7, 2] → 3

You can take 3 sweets, and there are exactly three flavors: 4, 7 and 2.

Example 2
sweets=[1, 2, 3, 4, 5, 6, 7, 8] → 4

Every flavor is different, but only 4 sweets may be taken, so 4 flavors.

Example 3
sweets=[-3, -3] → 1
Constraints
  • 2 ≤ n = len(sweets) ≤ 10⁴
  • n is even
  • -10⁵ ≤ sweets[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasyThe Candidate With Most Votes EasyDoes Any Value Repeat EasyRepeat Within Reach EasyThe Lost Ticket Easy
loading Python…
Input
sweets=[4, 4, 4, 7, 7, 2]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 100 tests on the server, hidden ones included.