Count Starting Numbers for Consecutive Sums
Math & Bit ManipulationAuto-graded · 24 test casesLast updated
Determine how many positive integers can serve as the starting element of a sequence of consecutive positive integers that sums to a target value.
A target positive integer s is given. We consider sequences of consecutive positive integers starting at some integer $k \ge 1$ with length $m \ge 1$, i.e., $[k, k+1, \dots, k+m-1]$.
Return the number of distinct positive integers $k$ for which there exists at least one choice of $m \ge 1$ such that the elements of the sequence sum to s.
Constraints
- $1 \le s \le 2 \times 10^9$
Examples
Example 1
Input: count_consecutive_sum_starts(10)
Output: 2
Example 2
Input: count_consecutive_sum_starts(9)
Output: 3
Example 3
Input: count_consecutive_sum_starts(15)
Output: 4
Example 4
Input: count_consecutive_sum_starts(1)
Output: 1
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The sum of $m$ consecutive terms starting at $k$ is given by the formula for an arithmetic progression: $$s = \sum_{i=0}^{m-1} (k + i) = m k + \frac{m(m-1)}{2}$$
Multiplying both sides by 2 gives: $$2s = m (2k + m - 1)$$
Notice that the two factors $A = m$ and $B = 2k + m - 1$ must multiply to $2s$. Furthermore:
- $B - A = 2k - 1$, which is always an odd integer. Thus, $A$ and $B$ must have opposite parity (one is even and the other is odd).
- Because $k \ge 1$, we have $B > A$.
Any factorization $2s = A \cdot B$ with different parities maps uniquely to a valid pair $(k, m)$ where:
- $m = \min(A, B)$
- $2k + m - 1 = \max(A, B) \implies k = \frac{\max(A, B) - \min(A, B) + 1}{2}$
Since $2s$ has factors of 2 from the initial factor of 2 in $2s$, the odd factor in the pair $(A, B)$ must be an odd divisor of $s$. There is a direct one-to-one correspondence between:
- Every valid starting integer $k$
- Every distinct sequence representation of $s$
- Every odd divisor of $s$
Hence, the total count of valid starting values $k$ equals the number of odd divisors of $s$.
To compute the number of odd divisors of $s$:
- Divide $s$ by 2 repeatedly to extract its odd component $o$.
- Find the prime factorization of $o = p_1^{e_1} p_2^{e_2} \dots p_r^{e_r}$.
- The number of odd divisors is given by $(e_1 + 1)(e_2 + 1)\dots(e_r + 1)$.
Complexity
- Time Complexity: $\mathcal{O}(\sqrt{s})$ in the worst case (when $s$ has a large prime factor). Dividing out factors of 2 takes $\mathcal{O}(\log s)$, and trial division up to $\sqrt{o}$ takes $\mathcal{O}(\sqrt{s})$.
- Space Complexity: $\mathcal{O}(1)$, as we only keep track of a few scalar integer variables.
def count_consecutive_sum_starts(s: int) -> int:
# Reduce s to its odd part
odd_part = s
while odd_part % 2 == 0:
odd_part //= 2
count = 1
factor = 3
# Trial division for odd prime factors up to sqrt(odd_part)
while factor * factor <= odd_part:
exponent = 0
while odd_part % factor == 0:
odd_part //= factor
exponent += 1
if exponent > 0:
count *= (exponent + 1)
factor += 2
# If there is a remaining prime factor greater than sqrt(s)
if odd_part > 1:
count *= 2
return count