Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For LeetCode 2929, count each valid ordered allocation by fixing the first child’s share and summing the feasible interval for the second child. This direct Elixir solution takes O(min(n, limit)) time and O(1) extra space; it returns 0 immediately when the three children’s combined capacity is too small.

What the problem asks

Given positive integers n and limit, distribute all n candies among three distinct children. Each child may receive zero candies, but no child may receive more than limit. Count the ordered allocations, so changing which child receives a share can create a different allocation. LeetCode lists constraints of 1 <= n <= 10^6 and 1 <= limit <= 10^6 on its problem page.

For example, the official examples are (n = 5, limit = 2), which returns 3, and (n = 3, limit = 3), which returns 10.

Count allocations by fixing the first child’s share

Let the first child receive i candies. The second child receives j, leaving n - i - j for the third. Both remaining shares must be between zero and limit, so the valid values of j are bounded by:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

max(0, n - i - limit) <= j <= min(limit, n - i)

The lower bound ensures the third child does not exceed the cap; the upper bound ensures the second child does not exceed it and that the third child is not negative. Every integer in this inclusive interval produces exactly one allocation for this fixed i. Its contribution is therefore max(0, upper - lower + 1).

The first child’s share must leave enough capacity for the other two and cannot exceed either the total or the per-child limit. Thus, enumerate i from max(0, n - 2 * limit) through min(n, limit), inclusive. If n > 3 * limit, the three children cannot hold all the candies and the answer is zero.

Elixir implementation

defmodule Solution do
  def distribute_candies(n, limit) do
    if n > 3 * limit do
      0
    else
      first_min = max(0, n - 2 * limit)
      first_max = min(n, limit)

      Enum.reduce(first_min..first_max, 0, fn i, total ->
        second_min = max(0, n - i - limit)
        second_max = min(limit, n - i)
        total + max(0, second_max - second_min + 1)
      end)
    end
  end
end

The early capacity check also ensures the enumerated first-share range is nonempty. Elixir integers use arbitrary precision, so this calculation does not need fixed-width overflow handling.

Check the examples

  • distribute_candies(5, 2) returns 3. The only valid ordered allocations are permutations of (1, 2, 2).
  • distribute_candies(3, 3) returns 10, the number of nonnegative ordered triples summing to three when the cap does not exclude any allocation.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Complexity and an alternative

The interval-sum method visits one value of i at a time, so it uses O(min(n, limit)) time and O(1) extra space. With LeetCode’s maximum input of one million for each parameter, this enumeration is a practical, transparent option.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Inclusion-exclusion is an O(1)-time alternative: start with the unrestricted count C(n + 2, 2), subtract allocations where a named child exceeds the cap, then add back pairwise overlaps. LeetCode’s solution listing presents that route alongside enumeration. It avoids iterating over a range, but translating the shifted binomial terms correctly requires careful boundary handling. For an Elixir implementation that favors visible bounds and straightforward checking, the interval sum is easier to audit.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.