TheAlgorithms/C-Sharp · error · ArgumentException

The step cannot be smaller than 1

Error message

The step cannot be smaller than 1

What it means

FindWinner(n, k) simulates the Josephus problem where k-1 people are skipped and the k-th is eliminated each round. The library throws ArgumentException when k < 1 because a step of zero or negative makes the elimination sequence undefined. The guard is validated up-front before the iterative recurrence runs.

Solutions

  1. Pass a step count k >= 1; a step of 1 means every person is eliminated in order.
  2. If k comes from user input or config, validate/clamp it to >= 1 before calling.
  3. If you intend 'remove every person' semantics, note that k=1 already does that; do not pass 0.

Example fix

// before
var winner = JosephusProblem.FindWinner(n, step - 1); // step-1 can be 0
// after
var winner = JosephusProblem.FindWinner(n, Math.Max(1, step));
Defensive patterns

Strategy: validation

Validate before calling

if (k < 1) throw new ArgumentOutOfRangeException(nameof(k), "Step must be >= 1");
var winner = JosephusProblem.FindWinner(n, k);

Try / catch

try { var w = JosephusProblem.FindWinner(n, k); }
catch (ArgumentException ex) when (ex.Message.Contains("step cannot be smaller")) { /* fall back to k = 1 or report invalid input */ }

Prevention

When it happens

Trigger: Calling JosephusProblem.FindWinner(n, k) with k <= 0, e.g. FindWinner(10, 0) or FindWinner(10, -3).

Common situations: Passing an uninitialized or default step count (0) computed from other logic, or translating a zero-based user input step into the 1-based k the algorithm expects.

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/e7197471e4e6d673. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Numeric/JosephusProblem.cs:15

namespace Algorithms.Numeric;

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)