Day 17 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#829. Consecutive Numbers Sum(Hard/JavaScript)
Tags: algorithms, javascript, beginners, programming
Published: 2022-02-28 04:19:30

**Intro:** I am a former accountant turned software engineer graduated from coding bootcamp in January 2022. Algorithms and Data Structure is an unavoidable part of interviews for most of the tech companies now. And one of my friends told me that you need to solve a medium leetcode problem under 60 seconds in order to get into the top tech companies.So I thought I'd start learning how to do it while job searching.

Since I have no clue on how to solve any of the problems (even the easy ones), I thought there is no point for me to waste hours and can't get it figured out. **Here is my approach:**
- Pick a leetcode problem randomly or Online Assessment from targeted companies.
- Study 1-2 solutions from Youtube or LeetCode discussion section. One brute force solution, another one more optimal.
- Write a blog post with detailed explanation and do a verbal walk through to help understand the solutions better.
- Code out the solution in LeetCode without looking at the solutions
- **Combat the forgetting curve:** Re-do the question for the next three days. And come back regularly to revisit the problem.

******************************************

**829. Consecutive Numbers Sum**
`Difficulty: Hard` `Language: JavaScript` 
### Another math problem
<!--Goldman Sachs OA-->

Given an integer `n`, return the number of ways you can write `n` as the sum of consecutive positive integers.

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 <= 109`

******************************************

**Solution:**
I found three difference solutions and below solution has less code, is easy to understand/explain and it runs faster and takes less space. I attached youtube link in the reference section. Key to this problem is to find a pattern in the numbers like all other Math problems on LeetCode. To further analyze example 3 above, one of the combination is 4 + 5 + 6. We can break it down to:
(3 + 1) + (3 + 2) + (3 + 3) = 15. The set exists when (15 - 1 - 2 - 3) is divisible by 3 has no remainder. With this pattern, we will keep subtracting 4,5...i from 15 until n = 1. Whenever remaining n is divisible by i, there is a working ways you can write 15 as the sum of consecutive positive integers
```
var consecutiveNumbersSum = function (n) {

    let count = 0, i = 1;

    while (n > 0){

//while (note 1) n is greater than 0

        n-=i;

//subtract i from n (note 2)

        if(n % i ==0) 

//if (n-i) is divisible (note 4) by i, a working combinaion is found

        count ++;
        i++;

//increase (note 3) count to record numbers of ways found to write
//n as the sum of consecutive positive integers. Increase i by 1
//to test out next possibility.

    }
    return count
};
```
**Solution Submission detail as of 2/27/2022** 
(Data below could vary since there are new tests/submissions daily)
- Runtime: 91 ms
- Memory Usage: 41.9 mb

******************************************

**References:**
[LeetCode Problem Link](https://leetcode.com/problems/consecutive-numbers-sum/)
[Youtube: Aslan Tashtanov](https://www.youtube.com/watch?v=UXoYfLrxqI0)
[Note 1: While loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)
[Note 2: Subtraction assignment (-=)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Subtraction_assignment)
[Note 3: Increasement(++)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment)
[Note 4: Remainder(%)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder)
[Blog Cover Image Credit](https://www.istockphoto.com/portfolio/monsitj?mediatype=photography)