TheAlgorithms/C-Sharp · error · ArgumentException
The step cannot be greater than the size of the group
Error message
The step cannot be greater than the size of the group
What it means
FindWinner(n, k) requires the step k to be no larger than the group size n; with k > n more than n-1 people would be skipped each round, which is undefined for the Josephus simulation. The library throws ArgumentException to reject such inputs before running the recurrence. Note n is also expected to be a positive group size.
Solutions
- Ensure the arguments are in the right order: FindWinner(groupSize, step).
- Validate k <= n before calling, or clamp k to n.
- If the group may be empty, decide policy first: the call with k>n is invalid, so guard the caller side.
Example fix
// before var winner = JosephusProblem.FindWinner(k, n); // swapped args // after var winner = JosephusProblem.FindWinner(n, k);
Defensive patterns
Strategy: validation
Validate before calling
if (n < 1 || k < 1 || k > n) throw new ArgumentOutOfRangeException(nameof(k), "Require 1 <= k <= n"); var winner = JosephusProblem.FindWinner(n, k);
Try / catch
try { var w = JosephusProblem.FindWinner(n, k); }
catch (ArgumentException ex) when (ex.Message.Contains("greater than the size")) { /* fix argument order or clamp k */ } Prevention
- Double-check argument order: (groupSize, step), not (step, groupSize)
- Derive n from the same collection whose count you intend to simulate
- Assert k <= n in tests with boundary cases k == n and k == 1
When it happens
Trigger: Calling JosephusProblem.FindWinner(n, k) with k > n, e.g. FindWinner(5, 10).
Common situations: Swapping the two parameters by mistake (passing the step as n), or computing n from a filtered/empty list while keeping a fixed step size.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- The step cannot be smaller than 1
- n
- Collections must have equal count
- Invalid parameter settings for Ascon Hash
- Cash flows list cannot be empty
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/99b41a2c671f491e.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Numeric/JosephusProblem.cs:20
public static class JosephusProblem
{
/// <summary>
/// Calculates the winner in the Josephus problem.
/// </summary>
/// <param name="n">The number of people in the initial circle.</param>
/// <param name="k">The count of each step. k-1 people are skipped and the k-th is executed.</param>
/// <returns>The 1-indexed position where the player must choose in order to win the game.</returns>
public static long FindWinner(long n, long k)
{
if (k <= 0)
{
throw new ArgumentException("The step cannot be smaller than 1");
}
if (k > n)
{
throw new ArgumentException("The step cannot be greater than the size of the group");
}
long winner = 0;
for (long stepIndex = 1; stepIndex <= n; ++stepIndex)
{
winner = (winner + k) % stepIndex;
}
return winner + 1;
}
}
View on GitHub (pinned to 96e2905cab)