TheAlgorithms/C-Sharp · error
cannot contain numbers less than or equal to zero
Error message
{nameof(coinsAsArray)} cannot contain numbers less than or equal to zero What it means
ValidateCoinsArray rejects coin systems containing zero or negative denominations, since coin values must be positive integers for change-making to be meaningful. It throws InvalidOperationException naming the offending parameter coinsAsArray.
Solutions
- Filter out non-positive values: coins.Where(c => c > 0) before calling.
- Fix the data source that produced 0/negative denominations.
- Add caller-side validation with a clear error message naming the bad entry.
Example fix
// before var coins = ParseCoins(userInput); // may contain 0 solver.GenerateSingleCoinChanges(coins, amount); // after var coins = ParseCoins(userInput).Where(c => c > 0).Distinct().ToArray(); solver.GenerateSingleCoinChanges(coins, amount);
Defensive patterns
Strategy: validation
Validate before calling
if (coins != null && coins.Any(c => c <= 0)) throw new ArgumentException("All coin values must be positive.", nameof(coins)); Try / catch
try { solver.GenerateSingleCoinChanges(coins, amount); }
catch (InvalidOperationException ex) { Console.Error.WriteLine($"Invalid coin values: {ex.Message}"); } Prevention
- Sanitize parsed input with c > 0 filter
- Never pass raw user input as denominations
- Validate at the data-loading boundary
When it happens
Trigger: Calling GenerateSingleCoinChanges with coins such as [0, 1, 5] or [-1, 2, 5] — any element <= 0.
Common situations: Parsing denominations from user input or a file where an empty/0 default slipped in; integer parsing of malformed rows yielding negatives; uninitialized placeholder entries in the array.
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
- Coins array must contain coin 1
- Coins array cannot contain duplicates
- message
- key must be non-empty string
- k must be at least 1.
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/667726b3a7a31749.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Problems/DynamicProgramming/CoinChange/DynamicCoinChangeSolver.cs:158
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)