Consecutive Positive Integer Sums
Updated
Problem statement
Count the ways to write n as a sum of one or more consecutive positive integers.
This is the classic consecutive-sum problem. 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) → intExamples
Example 1
n = 5return = 2The representations are 5 and 2 + 3.
Example 2
n = 9return = 3The representations are 9, 4 + 5 and 2 + 3 + 4.
Example 3
n = 15return = 4The 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 Online Assessment questions
- Centered Block Placement QueriesOA · Seen Oct 2026
- Robot Navigation Around LasersOA · Seen Oct 2026
- Factory Batch Profit (Game Adaptation)OA · Seen Sep 2026
- Nested Recipe Blocks (Game Adaptation)OA · Seen Sep 2026
- Vehicle Builds for Required Terrain (Game Adaptation)OA · Seen Sep 2026
- Track Received Byte RangesOA · Seen Sep 2026
- Inventory Discount TrackerOA · Seen Sep 2026
- Break a PalindromeOA · Seen Jun 2026
- Count Pairs Differing in One DigitOA · Seen Oct 2025
- Find Zero Cells in a 2×2 GridOA · Seen Oct 2025
Similar questions at other companies
- Count Distinct Arrays After Decrement OperationsBloomberg LP · OABloomberg LP interview questions
- Polynomial CoefficientsShopify · OAShopify interview questions
- Basic Calculator IITypeSafe AI · OA
- Last Object to Fall Off the TableConfluent · OAConfluent interview questions
- Last Object Exit TimeIBM · OAIBM interview questions
- Count Prime Values in SubtreesVisa · OAVisa interview questions
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Server Upgrade PlanningWells Fargo · OAWells Fargo interview questions
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Gaussian Naive Bayes Classifier (Machine Learning OA)Pinterest · OAPinterest interview questions