Skip to content

Commit 38f8633

Browse files
committedMar 18, 2025
Add solution #829
1 parent a14235a commit 38f8633

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@
637637
826|[Most Profit Assigning Work](./0826-most-profit-assigning-work.js)|Medium|
638638
827|[Making A Large Island](./0827-making-a-large-island.js)|Hard|
639639
828|[Count Unique Characters of All Substrings of a Given String](./0828-count-unique-characters-of-all-substrings-of-a-given-string.js)|Hard|
640+
829|[Consecutive Numbers Sum](./0829-consecutive-numbers-sum.js)|Hard|
640641
830|[Positions of Large Groups](./0830-positions-of-large-groups.js)|Easy|
641642
831|[Masking Personal Information](./0831-masking-personal-information.js)|Medium|
642643
841|[Keys and Rooms](./0841-keys-and-rooms.js)|Medium|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 829. Consecutive Numbers Sum
3+
* https://leetcode.com/problems/consecutive-numbers-sum/
4+
* Difficulty: Hard
5+
*
6+
* Given an integer n, return the number of ways you can write n as the sum of consecutive
7+
* positive integers.
8+
*/
9+
10+
/**
11+
* @param {number} n
12+
* @return {number}
13+
*/
14+
var consecutiveNumbersSum = function(n) {
15+
let result = 0;
16+
17+
for (let k = 1; k < Math.sqrt(2 * n); k++) {
18+
const numerator = n - (k * (k - 1)) / 2;
19+
if (numerator % k === 0) {
20+
const startingNum = numerator / k;
21+
if (startingNum > 0) {
22+
result++;
23+
}
24+
}
25+
}
26+
27+
return result;
28+
};

0 commit comments

Comments
 (0)
Please sign in to comment.