Consecutive Numbers Sum
Real-world version: Rows of Increasing SeatsThe statement here is our own: the same inputs, outputs and technique.
A hall has exactly n seats and they all have to be used. The seats are laid out in one or more rows, read from the front of the hall to the back, under two rules: every row holds at least one seat, and each row after the first holds exactly one seat more than the row directly in front of it.
Two layouts are different when they use a different number of rows, or when some row holds a different number of seats. A single row holding all n seats is a valid layout.
Return how many different layouts the hall allows.
Examples
Example 1
- Input
- n = 12
- Output
- 2
Two layouts work: one row of 12, and the three rows 3, 4, 5 which also total 12.
Example 2
- Input
- n = 27
- Output
- 4
The four layouts are the single row 27, the two rows 13 and 14, the three rows 8, 9 and 10, and the six rows 2 through 7.
Example 3
- Input
- n = 4
- Output
- 1
Only the single row of 4 works. A two-row layout would need a front row and a back row one larger that add to 4, and no whole number does that, while the smallest three-row layout already totals 1 + 2 + 3 = 6.
Constraints
- 1 <= n <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def consecutive_numbers_sum(n: int) -> int:public int consecutiveNumbersSum(int n)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Smallest Divisible Digit Product IReal-world version: Next Shelf LabelEasy
- Count Square Sum TriplesReal-world version: Triples of Squares That Add UpEasy
- Count PrimesReal-world version: Indivisible Catalogue NumbersMedium
- Total Waviness of Numbers in Range IReal-world version: Serial Number KinksMedium