Problem · Math
Count Targets Formed by K Consecutive Positive Integers
Learn this problemProblem statement
Given an inclusive target range [start, end] and a positive integer k, count the targets in the range that can be written as the sum of exactly k consecutive positive integers.
For example, a valid target has the form x + (x + 1) + ... + (x + k - 1) for some integer x >= 1.
Function
countConsecutiveSumTargets(start: long, end: long, k: int) → longExamples
Example 1
start = 1end = 20k = 3return = 5The valid sums are 6, 9, 12, 15, and 18.
Example 2
start = 10end = 30k = 2return = 10The odd targets 11 through 29 are sums of two consecutive positive integers.
Example 3
start = 1end = 5k = 4return = 0The smallest four-term positive sum is 10, outside the range.
Constraints
1 <= start <= end <= 10^181 <= k <= 10^9