{"record":{"id":"e7197471e4e6d673","repo":"TheAlgorithms/C-Sharp","slug":"the-step-cannot-be-smaller-than-1","errorCode":null,"errorMessage":"The step cannot be smaller than 1","messagePattern":"The step cannot be smaller than 1","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/JosephusProblem.cs","lineNumber":15,"sourceCode":"﻿namespace Algorithms.Numeric;\n\npublic static class JosephusProblem\n{\n    /// <summary>\n    /// Calculates the winner in the Josephus problem.\n    /// </summary>\n    /// <param name=\"n\">The number of people in the initial circle.</param>\n    /// <param name=\"k\">The count of each step. k-1 people are skipped and the k-th is executed.</param>\n    /// <returns>The 1-indexed position where the player must choose in order to win the game.</returns>\n    public static long FindWinner(long n, long k)\n    {\n        if (k <= 0)\n        {\n            throw new ArgumentException(\"The step cannot be smaller than 1\");\n        }\n\n        if (k > n)\n        {\n            throw new ArgumentException(\"The step cannot be greater than the size of the group\");\n        }\n\n        long winner = 0;\n        for (long stepIndex = 1; stepIndex <= n; ++stepIndex)\n        {\n            winner = (winner + k) % stepIndex;\n        }\n\n        return winner + 1;\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/JosephusProblem.cs#L1-L32","documentation":"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.","triggerScenarios":"Calling JosephusProblem.FindWinner(n, k) with k <= 0, e.g. FindWinner(10, 0) or FindWinner(10, -3).","commonSituations":"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.","solutions":["Pass a step count k >= 1; a step of 1 means every person is eliminated in order.","If k comes from user input or config, validate/clamp it to >= 1 before calling.","If you intend 'remove every person' semantics, note that k=1 already does that; do not pass 0."],"exampleFix":"// before\nvar winner = JosephusProblem.FindWinner(n, step - 1); // step-1 can be 0\n// after\nvar winner = JosephusProblem.FindWinner(n, Math.Max(1, step));","handlingStrategy":"validation","validationCode":"if (k < 1) throw new ArgumentOutOfRangeException(nameof(k), \"Step must be >= 1\");\nvar winner = JosephusProblem.FindWinner(n, k);","typeGuard":null,"tryCatchPattern":"try { var w = JosephusProblem.FindWinner(n, k); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"step cannot be smaller\")) { /* fall back to k = 1 or report invalid input */ }","preventionTips":["Clamp user-supplied step counts to >= 1 at the input boundary","Remember k is 1-based: a step of 1 eliminates every person in order","Add unit tests covering k = 0 and negative k"],"tags":["argument-validation","algorithm","csharp"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}