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

Code
n = 10

Output

Code
1

Explanation

Code
10 = 1 + 2 + 3 + 4

There is only one valid representation.

Example 2

Input

Code
n = 15

Output

Code
3

Explanation

Code
15 = 1 + 2 + 3 + 4 + 5
15 = 4 + 5 + 6
15 = 7 + 8

Hence, the answer is 3.

Understanding the Problem

We need to count all possible sequences of consecutive natural numbers whose sum equals n.

For example:

Code
n = 21

21 = 10 + 11
21 = 6 + 7 + 8
21 = 1 + 2 + 3 + 4 + 5 + 6

Answer = 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

Code
1
1+2
1+2+3
1+2+3+4
1+2+3+4+5 = 15 ✔

Now start from 2

Code
2
2+3
2+3+4
2+3+4+5
...

Continue until all starting numbers are checked.

Complexity

Code
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.

Code
x
x+1
x+2
...
x+k-1

Its sum is:

Code
x + (x+1) + (x+2) + ... + (x+k-1)

Using the arithmetic progression formula:

Code
Sum = k(2x + k - 1) / 2

Since the sum equals n:

Code
n = k(2x + k - 1) / 2

Multiply both sides by 2:

Code
2n = k(2x + k - 1)

Now solve for x:

Code
x = (2n/k - k + 1) / 2

Now observe:

For every possible sequence length k, we only need to check whether:

  • 2n is divisible by k.

  • x is 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:

Code
1 + 2 + 3 + ... + k

which equals:

Code
k(k + 1) / 2

If this minimum sum becomes larger than n, no longer sequence is possible.

Therefore:

Code
k(k + 1) / 2 <= n

This limits the loop to approximately:

Code
√(2n)

Hence the algorithm runs in O(√n) time.

Java Solution

Java
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

Java
int count = 0;

Stores the number of valid ways.

Initially:

Code
count = 0

Step 2

Java
int twoN = 2 * n;

Instead of calculating 2*n repeatedly, we store it once.

Example:

Code
n = 15

twoN = 30

Step 3

Java
for (int k = 2; k * (k + 1) / 2 <= n; k++)

We try every possible sequence length.

For:

Code
n = 15

Possible values are:

Code
k = 2
k = 3
k = 4
k = 5

When:

Code
k = 6
Code
6 × 7 / 2 = 21
Code
21 > 15

Stop.

Step 4

Java
if (twoN % k != 0)
    continue;

From the formula:

Code
2n = k(2x + k - 1)

k must divide 2n.

Otherwise, no integer solution exists.

Example:

Code
n = 15

2n = 30

k = 4
Code
30 % 4 = 2

Not divisible.

Skip.

Step 5

Java
int temp = twoN / k - k + 1;

This computes:

Code
2x

because:

Code
2x = (2n / k) - k + 1

Example:

Code
n = 15

k = 3
Code
temp = 30 / 3 - 3 + 1
     = 10 - 3 + 1
     = 8

Step 6

Java
if (temp > 0 && temp % 2 == 0)

We check:

  • Starting number is positive.

  • Starting number is an integer.

Since:

Code
x = temp / 2

Example:

Code
temp = 8

x = 4

Sequence:

Code
4
5
6

Sum:

Code
4 + 5 + 6 = 15

Increase count.

Step 7

Java
count++;

One valid sequence has been found.

Step 8

Java
return count;

Return the final answer.

Dry Run

Input

Code
n = 15
Code
count = 0
twoN = 30

k = 2

Code
30 % 2 = 0
Code
temp = 15 - 2 + 1
     = 14

Even.

Code
x = 7

Sequence:

Code
7 + 8
Code
count = 1

k = 3

Code
30 % 3 = 0
Code
temp = 10 - 3 + 1
     = 8
Code
x = 4

Sequence:

Code
4 + 5 + 6
Code
count = 2

k = 4

Code
30 % 4 != 0

Skip.

k = 5

Code
30 % 5 = 0
Code
temp = 6 - 5 + 1
     = 2
Code
x = 1

Sequence:

Code
1 + 2 + 3 + 4 + 5
Code
count = 3

Stop.

Code
k = 6

6 × 7 / 2 = 21

21 > 15

Answer

Code
3

Dry Run for n = 10

Code
twoN = 20

k = 2

Code
20 % 2 = 0
Code
temp = 10 - 2 + 1
     = 9

Odd.

Invalid.

k = 3

Code
20 % 3 != 0

Skip.

k = 4

Code
20 % 4 = 0
Code
temp = 5 - 4 + 1
     = 2

Even.

Code
x = 1

Sequence:

Code
1 + 2 + 3 + 4
Code
count = 1

Answer

Code
1

Complexity Analysis

ComplexityValue
Time ComplexityO(√n)
Space ComplexityO(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 to O(√n) while using O(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.