Leetcode 829. Consecutive Numbers Sum

Problem Description

Given a positive integerย N, we are required to determine the number of ways we can represent it as a sum ofย consecutive positive integers. It can be written in exactly one way as a sum of itself (i.e. N = N). We need to find any additional representations.ย For example, for N=5, there are 2 ways:ย 5 = 5 and 5 = 2 + 3.

Solution Approach

The idea here is to represent N as a sequence of consecutive positive integers in k terms:

N = x + (x+1) + (x+2) + .... + k terms

This can also be written as N = k*x + (k*(k-1)/2), which simplifies further to N = k*(x + (k-1)/2).

Therefore, we can iterate over k from 1 to N and for every k check if it is possible to form N using the above formula.

Now let's consider an example.

For N=9, using the formula and iterating over k, we'll get these possibilities:

9 = 9*(1 + (9-1)/2) 9 = 4*(2 + (4-1)/2) 9 = 2*(4 + (2-1)/2)

These translate to 9 = 9, 9 = 4 + 5 and 9 = 2 + 3 + 4, giving 3 possible representations.

Solutions in Different Languages

Python

1
2python
3class Solution:
4    def consecutiveNumbersSum(self, N: int) -> int:
5        count = 0
6        # Iterate over possible number of terms
7        for k in range(1, int(((2*N)**0.5)+1)):
8            # Verify if it fits the formula for sum of k terms
9            if (N - k*(k-1)//2) % k == 0:
10                count += 1
11        return count

Java

1
2java
3class Solution {
4    public int consecutiveNumbersSum(int N) {
5        int count = 0;
6        for (int k = 1; k < Math.sqrt(2 * N); k++) {
7            if ((N - (k * (k - 1)) / 2) % k == 0) {
8                count++;
9            }
10        }
11        return count;
12    }
13}

JavaScript

1
2javascript
3var consecutiveNumbersSum = function(N) {
4    let count = 0;
5    for (let k=1; k< Math.sqrt(2*N); k++) {
6        if ((N - k*(k-1)/2) % k === 0) {
7            count++;
8        }
9    }
10    return count;
11};

C++

1
2cpp
3class Solution {
4public:
5    int consecutiveNumbersSum(int N) {
6        int count = 0;
7        for (int k=1; k< sqrt(2*N); k++) {
8            if ((N - k*(k-1)/2) % k == 0) {
9                count++;
10            }
11        }
12        return count;
13    }
14};

C#

1
2csharp
3public class Solution {
4    public int ConsecutiveNumbersSum(int N) {
5        int count = 0;
6        for (int k=1; k< Math.Sqrt(2*N); k++) {
7            if ((N - k*(k-1)/2) % k == 0) {
8                count++;
9            }
10        }
11        return count;
12    }
13}

Complexity Analysis

Time Complexity

The time complexity of the above solution is O(sqrt(N)) because we are iterating until sqrt(2*N) for possible number of terms in the representation of N.

Space Complexity

The space complexity is O(1). There are no additional data structures being used that scale with the size of the input.

Conclusion

With some mathematical transformations, we've been able to considerably reduce the problem's complexity. We've found that a positive integer can be represented as a sum of consecutive positive integers using an efficient formula. Our solution calculates representations in O(sqrt(N)) time, which can be considered efficient for fairly large values of N. This solution has the added benefit of being easy to implement in multiple programming languages, including Python, Java, JavaScript, C++, and C#.


Got a question?ย Ask the Teaching Assistantย anything you don't understand.

Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.

โ†
โ†‘TA ๐Ÿ‘จโ€๐Ÿซ