Consecutive Numbers Sum

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

Example 1
Inputn = 5
Output2
The number 5 can be written as 5 and as 2 + 3, so there are 2 ways.
Example 2
Inputn = 9
Output3
The number 9 can be written as 9, 4 + 5, and 2 + 3 + 4, so there are 3 ways.

Constraints

  • 1 <= n <= 10^9

Asked at 8 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate