KatInterview

Count Starting Numbers for Consecutive Sums

Math & Bit ManipulationAuto-graded · 24 test cases
MediumAuto-gradedMath & Bit Manipulationmathnumber-theoryarithmetic-progression

Last 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