Consecutive Numbers Sum
Hard
Topics
Given an integer n, return the number of ways to write it as a sum of consecutive positive integers.
Example 1
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.Input: n = 5 Output: 2 Explanation: 5 = 5 = 2 + 3.
Example 2
Input: n = 9 Output: 3 Explanation: 9 = 9 = 4 + 5 = 2 + 3 + 4.
Example 3
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
- Start
kat 1. - While
k(k-1)/2 < n, computerem = n - k(k-1)/2. - Count this
kwhenremis divisible byk. - 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 4Complexity
- 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 <= nor similar. The triangular offset for a run of lengthkstarting ataisk(k-1)/2, notk(k+1)/2; getting it wrong shifts every answer by one run. - Allowing
ato be zero or negative. The problem says positive integers, which is exactly whatrem > 0enforces. - Forgetting that
nitself counts as a run of length 1. It falls out ofk = 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 countimport java.util.*;
class Solution {
public int consecutiveNumbersSum(int n) {
int count = 0;
long k = 1;
while (k * (k - 1) / 2 < n) {
long rem = n - k * (k - 1) / 2;
if (rem % k == 0) count++;
k++;
}
return count;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int consecutiveNumbersSum(int n) {
long long count = 0, k = 1;
while (k * (k - 1) / 2 < n) {
long long rem = (long long)n - k * (k - 1) / 2;
if (rem % k == 0) count++;
k++;
}
return (int)count;
}
};/**
* @param {number} n
* @return {number}
*/ var consecutiveNumbersSum = function(n) {
let count = 0, k = 1;
while (k * (k - 1) / 2 < n) {
const rem = n - k * (k - 1) / 2;
if (rem % k === 0) count++;
k++;
}
return count;
};