Show problems as
Classic problem #829HardMathEnumeration

Consecutive Numbers Sum

Real-world version: Rows of Increasing Seats
Tracked in this browser only
Read Consecutive Numbers Sum on LeetCode(opens in a new tab)

The 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.

Python
def consecutive_numbers_sum(n: int) -> int:
Java
public int consecutiveNumbersSum(int n)