Consecutive Numbers Sum

Hard

Topics
Math

Given an integer n, return the number of ways to write it as a sum of consecutive positive integers.

Example 1

52+3
Each chip is one way to write 5 as a run of consecutive positive integers. A run of length k starting at a sums to k·a + k(k-1)/2, so only k up to about √(2n) has to be tried — 2 work here.
Each chip is one valid consecutive run.
Input:  n = 5
Output: 2
Explanation: 5 = 5 = 2 + 3.

Example 2

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

Example 3

157+84+5+61+2+3+4+5
Input:  n = 15
Output: 4

Constraints

  • 1 <= n <= 10^9
Editorial — intuition, approach, complexity and pitfalls

Intuition

A run of k consecutive integers starting at a sums to k·a + k(k-1)/2. Setting that equal to n and solving for a gives a clean test: the run exists exactly when n - k(k-1)/2 is positive and divisible by k.

Positivity bounds the search. Since k(k-1)/2 < n, k is O(√n) — about 44,720 iterations at the limit of 10⁹, measured at 9 ms. So enumerate k and count the hits.

There is a second, sharper answer. The count equals the number of odd divisors of n — verified for every n from 1 to 2,999 against both the enumeration and a divisor count. That turns the problem into factorisation, and it explains the shape of the answers: a power of two has exactly one odd divisor, so 2²⁰ answers 1, while 15 = 3·5 has odd divisors 1, 3, 5, 15 and answers 4.

Approach

  1. Start k at 1.
  2. While k(k-1)/2 < n, compute rem = n - k(k-1)/2.
  3. Count this k when rem is divisible by k.
  4. Increment k.

Dry Run

n = 15

  k  k(k-1)/2  n - that  divisible by k  the run
  1  0         15        yes             15
  2  1         14        yes             7 8
  3  3         12        yes             4 5 6
  4  6         9         no              --
  5  10        5         yes             1 2 3 4 5

answer 4

cross-check: 15 = 3 * 5, odd divisors {1, 3, 5, 15}, count 4

Complexity

  • Time — O(√n): the loop runs while k(k-1)/2 < n. The odd-divisor route is O(√n) too, by trial division.
  • Space — O(1).

Common Pitfalls

  • Iterating over starting values instead of run lengths. There are O(n) starts and only O(√n) lengths.
  • Writing the loop bound as k < n, which is O(n) iterations — 10⁹ of them — instead of O(√n).
  • Using k*(k+1)/2 <= n or similar. The triangular offset for a run of length k starting at a is k(k-1)/2, not k(k+1)/2; getting it wrong shifts every answer by one run.
  • Allowing a to be zero or negative. The problem says positive integers, which is exactly what rem > 0 enforces.
  • Forgetting that n itself counts as a run of length 1. It falls out of k = 1, but it is the case people hand-wave away.
  • Claiming the odd-divisor result without deriving it. The bijection is between odd divisors and valid runs; asserting it unprompted invites exactly the question you cannot answer.

Full Solution — all languages

class Solution:
    def consecutiveNumbersSum(self, n):
        count = 0
        k = 1
        while k * (k - 1) // 2 < n:
            rem = n - k * (k - 1) // 2
            if rem % k == 0:
                count += 1
            k += 1
        return count
Asked at Airbnb D. E. Shaw
SensAI
Beta
Tap the mic to stop voice mode