Consecutive Numbers Sum
Problem Descriptionβ
Given an integer n
, return the number of ways you can write n
as the sum of consecutive positive integers.
Examplesβ
Example 1:
Input: n = 5
Output: 2
**Explanation**: 5 = 2 + 3
Example 2:
Input: n = 9
Output: 3
**Explanation:** 9 = 4 + 5 = 2 + 3 + 4
Example 3:
Input: n = 15
Output: 4
**Explanation:** 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5
Constraintsβ
1 <= n <= 10^9
Solution for Assign Cookiesβ
Approach:β
1- Discard all factors 2 from N.
2- Check all odd number i if N is divided by i.
3- Calculate the count of factors i that N has.
4- Update res *= count.
5- If N==1, we have found all primes and just return res.
6- Otherwise, N will be equal to P and we should do res *= count + 1 where count = 1.
Code in Different Languagesβ
C++β
class Solution {
public:
int consecutiveNumbersSum(int N) {
int res = 1, count;
while (N % 2 == 0) N /= 2;
for (int i = 3; i * i <= N; res *= count + 1, i += 2)
for (count = 0; N % i == 0; N /= i, count++);
return N == 1 ? res : res * 2;
}
};
Javaβ
class Solution {
public int consecutiveNumbersSum(int N) {
int res = 1, count;
while (N % 2 == 0) N /= 2;
for (int i = 3; i * i <= N; i += 2) {
count = 0;
while (N % i == 0) {
N /= i;
count++;
}
res *= count + 1;
}
return N == 1 ? res : res * 2;
}
}
Pythonβ
class Solution(object):
def consecutiveNumbersSum(self, N):
res = 1
i = 3
while N % 2 == 0:
N /= 2
while i * i <= N:
count = 0
while N % i == 0:
N /= i
count += 1
res *= count + 1
i += 2
return res if N == 1 else res * 2
Complexity Analysisβ
Time Complexity: O(logN)β
Space Complexity: O(1)β
Referencesβ
- LeetCode Problem: Consecutive Numbers Sum