Problem · Math

Count Targets Formed by K Consecutive Positive Integers

Learn this problem
MediumArm logoArmNEW GRADOA

Problem 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) → long

Examples

Example 1

start = 1end = 20k = 3return = 5

The valid sums are 6, 9, 12, 15, and 18.

Example 2

start = 10end = 30k = 2return = 10

The odd targets 11 through 29 are sums of two consecutive positive integers.

Example 3

start = 1end = 5k = 4return = 0

The smallest four-term positive sum is 10, outside the range.

Constraints

  • 1 <= start <= end <= 10^18
  • 1 <= k <= 10^9

More Arm problems

drafts saved locally
public long countConsecutiveSumTargets(long start, long end, int k) {
    // Write your solution here.
}
start1
end20
k3
expected5
checking account