{"record":{"id":"99b41a2c671f491e","repo":"TheAlgorithms/C-Sharp","slug":"the-step-cannot-be-greater-than-the-size-of-the-group","errorCode":null,"errorMessage":"The step cannot be greater than the size of the group","messagePattern":"The step cannot be greater than the size of the group","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/JosephusProblem.cs","lineNumber":20,"sourceCode":"\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":2,"sourceCodeEnd":32,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/JosephusProblem.cs#L2-L32","documentation":"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.","triggerScenarios":"Calling JosephusProblem.FindWinner(n, k) with k > n, e.g. FindWinner(5, 10).","commonSituations":"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.","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."],"exampleFix":"// before\nvar winner = JosephusProblem.FindWinner(k, n); // swapped args\n// after\nvar winner = JosephusProblem.FindWinner(n, k);","handlingStrategy":"validation","validationCode":"if (n < 1 || k < 1 || k > n) throw new ArgumentOutOfRangeException(nameof(k), \"Require 1 <= k <= n\");\nvar winner = JosephusProblem.FindWinner(n, k);","typeGuard":null,"tryCatchPattern":"try { var w = JosephusProblem.FindWinner(n, k); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"greater than the size\")) { /* fix argument order or clamp k */ }","preventionTips":["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"],"tags":["argument-validation","algorithm","csharp"],"backgroundTag":"value-out-of-range","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"}