[ OK ] meta-code-51 — full content available
[ INFO ] category: Coding difficulty: medium freq: Optional first seen: 2026-03-13
[MEDIUM][CODING][OPTIONAL]
$ cat problem.md
Practice/Meta/Leetcode 829. Consecutive Numbers Sum
Leetcode 829. Consecutive Numbers Sum
CodingOptional
Problem
Given a positive integer n, determine how many distinct ways you can express it as the sum of consecutive positive integers.
For example, the number 9 can be expressed in three ways:
- 9 (single number)
- 4 + 5 (two consecutive numbers)
- 2 + 3 + 4 (three consecutive numbers)
Your task is to count all such possible representations.
Requirements
- Return the total count of ways to express
n as a sum of consecutive positive integers
- A single number counts as one way (sequence of length 1)
- All numbers in the sequence must be positive integers
- The solution must handle large values of
n efficiently
Constraints
- 1 ≤ n ≤ 10^9
- Time complexity should be O(√n) or better
- Space complexity should be O(1)
Examples
Example 1:
`
Input: n = 5
Output: 2
Explanation:
- 5 (one number)
- 2 + 3 (two consecutive numbers)
`
Example 2:
`
Input: n = 9
Output: 3
Explanation:
- 9 (one number)
- 4 + 5 (two consecutive numbers)
- 2 + 3 + 4 (three consecutive numbers)
`
Example 3:
`
Input: n = 15
Output: 4
Explanation:
- 15 (one number)
- 7 + 8 (two consecutive numbers)
- 4 + 5 + 6 (three consecutive numbers)
- 1 + 2 + 3 + 4 + 5 (five consecutive numbers)
`