Talentd
FleetCode

829. Consecutive Numbers Sum

Hard43.2% acceptance7 companies

Given an integer n, return the number of ways you can write n as the sum of consecutive positive integers.

Example 1:

Input: n = 5
Output: 2
Explanation: 5 = 2 + 3

Example 2:

Input: n = 9
Output: 3
Explanation: 9 = 4 + 5 = 2 + 3 + 4

Example 3:

Input: n = 15
Output: 4
Explanation: 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5

Constraints:

  • 1 <= n <= 109
9:50
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def consecutiveNumbersSum(self, n: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
5
Input
9
Input
15
Your code is saved on this device.Solution