TheAlgorithms/C-Sharp · error

The coin cannot be lesser or equal to zero

Error message

The coin cannot be lesser or equal to zero {nameof(coin)}.

What it means

ValidateCoin is a private validation helper (a generic guard) used by GenerateSingleCoinChanges; it throws InvalidOperationException when a coin value is zero or negative, since change-making is undefined for non-positive denominations. The faulting input is the coin value.

Solutions

  1. Filter the coin denominations before solving so only positive values (>= 1) are passed in; note the solver also requires a coin of value 1.
  2. Fix the caller that computes or reads the coin value so it cannot yield 0 or negatives (e.g. from a bad parse or empty difference step).
  3. Replace the InvalidOperationException-based helper with argument validation up front so invalid coins are rejected before table generation starts.

When it happens

Trigger: Thrown at Algorithms/Problems/DynamicProgramming/CoinChange/DynamicCoinChangeSolver.cs:134 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/c6b491a86d7cd4c3. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Problems/DynamicProgramming/CoinChange/DynamicCoinChangeSolver.cs:134

        while (nextCoin != 0)
        {
            nextCoin = GetMinimalNextCoin(currentCoin, changesTable);
            var difference = currentCoin - nextCoin;
            list.Add(difference);
            currentCoin = nextCoin;
        }

        var result = list.ToArray();

        return result;
    }

    private static void ValidateCoin(int coin)
    {
        if (coin <= 0)
        {
            throw new InvalidOperationException($"The coin cannot be lesser or equal to zero {nameof(coin)}.");
        }
    }

    private static void ValidateCoinsArray(int[] coinsArray)
    {
        var coinsAsArray = coinsArray.ToArray();

        if (coinsAsArray.Length == 0)
        {
            throw new InvalidOperationException($"Coins array cannot be empty {nameof(coinsAsArray)}.");
        }

        var coinsContainOne = coinsAsArray.Any(x => x == 1);

        if (!coinsContainOne)
        {
            throw new InvalidOperationException($"Coins array must contain coin 1 {nameof(coinsAsArray)}.");
        }

View on GitHub (pinned to 96e2905cab)