Problem Statement
Given a positive integer n, find the number of ways to represent it as the sum of two or more consecutive natural numbers.
Example 1
Input
n = 10Output
1Explanation
10 = 1 + 2 + 3 + 4There is only one valid representation.
Example 2
Input
n = 15Output
3Explanation
15 = 1 + 2 + 3 + 4 + 5
15 = 4 + 5 + 6
15 = 7 + 8Hence, the answer is 3.
Understanding the Problem
We need to count all possible sequences of consecutive natural numbers whose sum equals n.
For example:
n = 21
21 = 10 + 11
21 = 6 + 7 + 8
21 = 1 + 2 + 3 + 4 + 5 + 6Answer = 3
Notice that:
Numbers must be consecutive.
Sequence length should be at least 2.
Starting number must be greater than 0.
Brute Force Approach
The simplest idea is:
Start from every number.
Keep adding consecutive numbers.
Stop when the sum exceeds
n.If the sum becomes exactly
n, increase the answer.
Example
For n = 15
Start from 1
1
1+2
1+2+3
1+2+3+4
1+2+3+4+5 = 15 ✔Now start from 2
2
2+3
2+3+4
2+3+4+5
...Continue until all starting numbers are checked.
Complexity
Time : O(n²)
Space: O(1)This is too slow for n = 10^8.
Efficient Mathematical Approach
Suppose a sequence starts from x and contains k consecutive numbers.
x
x+1
x+2
...
x+k-1Its sum is:
x + (x+1) + (x+2) + ... + (x+k-1)Using the arithmetic progression formula:
Sum = k(2x + k - 1) / 2Since the sum equals n:
n = k(2x + k - 1) / 2Multiply both sides by 2:
2n = k(2x + k - 1)Now solve for x:
x = (2n/k - k + 1) / 2Now observe:
For every possible sequence length k, we only need to check whether:
2nis divisible byk.xis a positive integer.
If both are true, one valid sequence exists.
Why Do We Iterate Only Up to √n?
The smallest sum of k consecutive numbers is:
1 + 2 + 3 + ... + kwhich equals:
k(k + 1) / 2If this minimum sum becomes larger than n, no longer sequence is possible.
Therefore:
k(k + 1) / 2 <= nThis limits the loop to approximately:
√(2n)Hence the algorithm runs in O(√n) time.
Java Solution
class Solution {
public int getCount(int n) {
int count = 0;
int twoN = 2 * n;
for (int k = 2; k * (k + 1) / 2 <= n; k++) {
// k must divide 2*n
if (twoN % k != 0)
continue;
int temp = twoN / k - k + 1;
// Starting number should be a positive integer
if (temp > 0 && temp % 2 == 0)
count++;
}
return count;
}
}Code Explanation
Step 1
int count = 0;Stores the number of valid ways.
Initially:
count = 0Step 2
int twoN = 2 * n;Instead of calculating 2*n repeatedly, we store it once.
Example:
n = 15
twoN = 30Step 3
for (int k = 2; k * (k + 1) / 2 <= n; k++)We try every possible sequence length.
For:
n = 15Possible values are:
k = 2
k = 3
k = 4
k = 5When:
k = 66 × 7 / 2 = 2121 > 15Stop.
Step 4
if (twoN % k != 0)
continue;From the formula:
2n = k(2x + k - 1)k must divide 2n.
Otherwise, no integer solution exists.
Example:
n = 15
2n = 30
k = 430 % 4 = 2Not divisible.
Skip.
Step 5
int temp = twoN / k - k + 1;This computes:
2xbecause:
2x = (2n / k) - k + 1Example:
n = 15
k = 3temp = 30 / 3 - 3 + 1
= 10 - 3 + 1
= 8Step 6
if (temp > 0 && temp % 2 == 0)We check:
Starting number is positive.
Starting number is an integer.
Since:
x = temp / 2Example:
temp = 8
x = 4Sequence:
4
5
6Sum:
4 + 5 + 6 = 15Increase count.
Step 7
count++;One valid sequence has been found.
Step 8
return count;Return the final answer.
Dry Run
Input
n = 15count = 0
twoN = 30k = 2
30 % 2 = 0temp = 15 - 2 + 1
= 14Even.
x = 7Sequence:
7 + 8count = 1k = 3
30 % 3 = 0temp = 10 - 3 + 1
= 8x = 4Sequence:
4 + 5 + 6count = 2k = 4
30 % 4 != 0Skip.
k = 5
30 % 5 = 0temp = 6 - 5 + 1
= 2x = 1Sequence:
1 + 2 + 3 + 4 + 5count = 3Stop.
k = 6
6 × 7 / 2 = 21
21 > 15Answer
3Dry Run for n = 10
twoN = 20k = 2
20 % 2 = 0temp = 10 - 2 + 1
= 9Odd.
Invalid.
k = 3
20 % 3 != 0Skip.
k = 4
20 % 4 = 0temp = 5 - 4 + 1
= 2Even.
x = 1Sequence:
1 + 2 + 3 + 4count = 1Answer
1Complexity Analysis
| Complexity | Value |
|---|---|
| Time Complexity | O(√n) |
| Space Complexity | O(1) |
Key Takeaways
The brute-force solution checks every possible sequence and takes
O(n²)time.The optimized approach uses the arithmetic progression formula to derive a condition for valid sequences.
By iterating only over possible sequence lengths (
k) up to√n, we reduce the time complexity toO(√n)while usingO(1)extra space.This makes the solution efficient enough for the constraint
n ≤ 10⁸.
Summary
To count the number of ways a positive integer can be expressed as the sum of two or more consecutive natural numbers, we can avoid brute-force enumeration by leveraging arithmetic progression properties. By deriving a mathematical condition for valid sequences and checking only feasible sequence lengths up to approximately √n, the solution achieves O(√n) time complexity and O(1) space complexity, making it efficient even for very large values of n.


Join the conversation! Your thoughts help the community grow.