File tree 2 files changed +29
-0
lines changed
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 637
637
826|[ Most Profit Assigning Work] ( ./0826-most-profit-assigning-work.js ) |Medium|
638
638
827|[ Making A Large Island] ( ./0827-making-a-large-island.js ) |Hard|
639
639
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|
640
641
830|[ Positions of Large Groups] ( ./0830-positions-of-large-groups.js ) |Easy|
641
642
831|[ Masking Personal Information] ( ./0831-masking-personal-information.js ) |Medium|
642
643
841|[ Keys and Rooms] ( ./0841-keys-and-rooms.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments