Distribute Candies LeetCode

In the world of sharing and fairness, the distribute candies challenge of LeetCode is really interesting. It’s about figuring out how to give a limited number of candies to a group of people so that everyone gets a fair amount, following certain rules. This problem is like real-life situations where we have to share resources fairly, like giving money to different departments of an organization or distributing supplies to people affected by some disaster.

Problem statement

We are given nn even number of candies of different types, and the type of candy ii is candyType[i]candyType[i]. Now, we have decided to share half of our candies with a sibling, and we want to give them the maximum types of candies so that they can taste the maximum.

Here is the list of requirements to remember:

  1. We have a total of nn candies.
  2. Candies are always even.
  3. The type of each candy is stored in the candyTypecandyType list.
  4. We can give only half of our candies.
  5. The given number of candies must contain the maximum types of candies.
Showcasing the scenario to understand the problem statement
Showcasing the scenario to understand the problem statement

Our task is to determine the maximum number of different candies that we can share. The result would be a number of different types of candies. Let’s try to map our problem into a visual for better understanding.

Educative-99 helps you solve complex coding problems like distribute candies by teaching you to recognize patterns and apply the right algorithms.

Algorithm

There are nn number of candies. Let’s assume cc as the type of candies. If the types of candies cc are less than half of the candies, we can choose all types of candies cc as output. If half of the candies are less than the total types of candies cc, we've to choose n2\frac{n}{2} types of candies.

Let’s write down the algorithm steps:

  1. Find the total number of candies to share (half of the total candies).

  2. Find the total number of types of candies (unique candies).

  3. If the types of candies are less than the total number of candies to be shared, return the total types of candies.

  4. Otherwise, return half of the total number of candies.

canvasAnimation-image
1 of 6

Knowledge test

Now you have understood the problem and are capable of finding the maximum types of candies to be shared.

Q

According to the above algorithm, how many types of candies can we share from the following collection of candies?

[[ 🍬, 🍭, 🍫, 🍡, 🍪, 🌰, 🍬, 🍬, 🍭, 🍫 ]]

A)

4

B)

5

C)

6

Coding example

Let’s implement the above algorithm below:

def distributeCandies(candyType: list[int]) -> int:
max_candies = len(candyType)//2 # finding the total number of candies to be shared
unique_candies = len(set(candyType)) # finding the total number of types of candies
# checking if the unique_candies are less than the maximum candies to be shared
if unique_candies < max_candies:
return unique_candies # return the unique_candies
return max_candies # otherwise return the max_candies to be shared
# ---------------------------------------------------------------------
candySet1 = [1, 1, 2, 2, 3, 3] # candy set 1
print("For the candy set:", candySet1, end="")
print(", we can share", distributeCandies(candySet1), "type(s) of candies!")
candySet2 = [1, 1, 2, 3] # candy set 2
print("For the candy set:", candySet2, end="")
print(", we can share", distributeCandies(candySet2), "type(s) of candies!")
candySet3 = [6, 6, 6, 6] # candy set 3
print("For the candy set:", candySet3, end="")
print(", we can share", distributeCandies(candySet3), "type(s) of candies!")
candySet4 = ['🍬', '🍭', '🍫', '🍡', '🍪', '🌰', '🍬', '🍬', '🍭', '🍫'] # candy set 4
print("For the candy set:", candySet4, end="")
print(", we can share", distributeCandies(candySet4), "type(s) of candies!")
Solution of the distribute candies problem in Python

Time complexity

In the above implementation, we use:

  • Finding the length takes the constant time: O(1)O(1).

  • Finding unique elements takes the linear time: O(n)O(n).

  • Condition check takes the constant time: O(1)O(1).

By the above distribution, the time complexity is linear: O(n)O(n).

Space complexity

In the above implementation, we use:

  • Two variables to store two integer values.

  • The set of unique candies to store at most nn elements.

By the above distribution, the space complexity is linear: O(n)O(n).

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved