FastPrepConsecutive Positive Integer Sums

Consecutive Positive Integer Sums

Roblox logoRoblox● MediumNEW GRADOA
Learn

Problem statement

Count the ways to write n as a sum of one or more consecutive positive integers.

For this exercise, assume the linked consecutive-sum baseline applies because the report does not describe its modification. A representation is determined by its starting positive integer and its length; a one-term representation containing only n is allowed.

Return the number of distinct representations. Zero and negative starting values are not allowed, and the terms must increase by exactly 1.

Function

countConsecutiveSums(n: int) → int

Examples

Example 1

n = 5return = 2

The representations are 5 and 2 + 3.

Example 2

n = 9return = 3

The representations are 9, 4 + 5 and 2 + 3 + 4.

Example 3

n = 15return = 4

The representations are 15, 7 + 8, 4 + 5 + 6 and 1 + 2 + 3 + 4 + 5.

Constraints

  • 1 <= n <= 10^9.
  • The result fits a signed int; use wider intermediate arithmetic where needed.

More Roblox problems

See Roblox hiring insights
public int countConsecutiveSums(int n) {
    // write your code here
}
n5
expected2
Checking account…