TheAlgorithms/C-Sharp · error

Coins array must contain coin 1

Error message

Coins array must contain coin 1 {nameof(coinsAsArray)}.

What it means

DynamicCoinChangeSolver.ValidateCoinsArray requires the coin system to include the coin value 1 so every amount can be represented. If coinsAsArray has no element equal to 1, the solver throws InvalidOperationException rather than returning incomplete change. This is an input preconditions check run before dynamic programming begins.

Solutions

  1. Add 1 to the coins array before calling the solver.
  2. If 1 is intentionally absent, switch to a solver variant that handles arbitrary coin systems (e.g. greedy/DP without the coin-1 precondition).
  3. Validate the coin list in caller code and produce a clearer domain-specific message.

Example fix

// before
var coins = new[] { 2, 5, 10 };
solver.GenerateSingleCoinChanges(coins, amount);
// after
var coins = new[] { 1, 2, 5, 10 };
solver.GenerateSingleCoinChanges(coins, amount);
Defensive patterns

Strategy: validation

Validate before calling

if (coins == null || !coins.Contains(1)) throw new ArgumentException("Coin system must include coin 1.", nameof(coins));

Try / catch

try { solver.GenerateSingleCoinChanges(coins, amount); }
catch (InvalidOperationException ex) { Console.Error.WriteLine($"Invalid coin system: {ex.Message}"); }

Prevention

When it happens

Trigger: Calling GenerateSingleCoinChanges (which invokes ValidateCoinsArray) with an array such as [2, 5, 10] that omits the value 1.

Common situations: Using a currency-like system with only large denominations (e.g. [5, 10, 25]); hand-trimming the coin list and accidentally dropping the 1-coin; loading denominations from config where 1 was filtered as trivial.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

        {
            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)}.");
        }

        var containsNonPositive = coinsAsArray.Any(x => x <= 0);

        if (containsNonPositive)
        {
            throw new InvalidOperationException(
                $"{nameof(coinsAsArray)} cannot contain numbers less than or equal to zero");
        }

        var containsDuplicates = coinsAsArray.GroupBy(x => x).Any(g => g.Count() > 1);

        if (containsDuplicates)
        {
            throw new InvalidOperationException($"Coins array cannot contain duplicates {nameof(coinsAsArray)}.");
        }
    }
}

View on GitHub (pinned to 96e2905cab)